I am writing an Alexa skill where I'd like to store records in a database with a future time. For example, if the user says "in two hours", I want to store the time two hours from now.
So far, I have discovered I can use an AMAZON.DURATION slot type to convert words like that into a duration, but I haven't yet figured out how I can add that to the current date.
The duration comes back like this: PT2H. The P indicates a duration, T indicates that it is time units, and the 2H is for 2 hours. I could parse this myself, but I was hoping there would be some built in function to do this?
So I resolved this using the moment.js library. I was already using this to format and work with dates, and I discovered they also support durations.
The way I resolved this was by getting the current date, parsing the duration, and adding them together:
var now = moment();
var duration = moment.duration(myDurationString);
var futureDate = now.add(duration);
Then, I could format futureDate
to be read back to the user and save it in the database.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With