Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add AMAZON.DURATION to current time

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?

like image 500
AdamMc331 Avatar asked Mar 05 '16 05:03

AdamMc331


1 Answers

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.

like image 177
AdamMc331 Avatar answered Sep 19 '22 13:09

AdamMc331