Dominique Charlebois Dominique Charlebois - 4 months ago 44
Javascript Question

Convert Date Value to Timestamp - Post Request - Ember.js

My handlebars code is as followed.

{{bootstrap-datepicker value=from placeholder="MM/DD/YYYY"}}


Here the user picks a date from a calendar to get the
from
date. This is for a form.

My problem is the format is as followed:
Sun May 29 2016 00:00:00 GMT-0700 (PDT)

and the post request requires milliseconds since epoch format.

SO

all the code sample I've seen to fix these problems, require the date in the function.

example:

var myDate="26-02-2012";
myDate=myDate.split("-");
var newDate=myDate[1]+"/"+myDate[0]+"/"+myDate[2];
alert(new Date(newDate).getTime());


or

function toTimestamp(year,month,day,hour,minute,second){
var datum = new Date(Date.UTC(year,month-1,day,hour,minute,second));
return datum.getTime()/1000;
}


or

var fromFormat = moment('from').fromFormat(moment(), 'milliseconds');


but I only have the name value as it changes all the time.

I've got moment-js, date-picker and a couple other add-ons. Any resources or code snippets will be appreciated.

Answer

So I finally figured it out with some help from friends. I created an action that took the formatted date from date-picker and transformed it into UNIX time in milliseconds.

actions: {
    PublishForm{
          var fromDate = moment(from).valueOf(); 
    }    
} 

**I've changed it this way due to issues displaying the date with the bootstrap-datepicker when you use the changeDate.

Comments