JavaScript Date

3

I have a date in the following format: 1424102400000 , that is, an integer value. (I do not know how to say the name of this format). Home I need to convert it to presentable value to the user, so far so good. I'm using the toUTCString () method and I can also use momentjs . So the previously displayed value is the same as Mon, 16 Feb 2015 16:00:00 GMT . Home The issue is that such a value presented to the user can be changed and then I need to convert it back to an integer value similar to the one I initially submitted (of course the value will be different if the user has made a change). Home Can someone help me? Home See this Fiddle , values do not beat!

    
asked by anonymous 22.07.2015 / 14:09

2 answers

3

If I understand correctly you want to convert from UNIX TimeStamp format to a display format and then convert back to what it was.

Solution - MomentJS

Use the method moment()

 var data = moment('seu valor em unix time stamp'); //retorna data

to return to its value in Unix Time Stamp use:

 var timeStamp = moment(data).ValueOf(); //retorna Timestamp * 1000

ValueOf () is equal to Unix () * 1000, because it shows in milliseconds

Documentation momentJS: link

Unix Time Stamp

The Unix Time Stamp format is the number of seconds of date difference from the date Jan 01 1970. (UTC)

Fix your fiddle: link

    
22.07.2015 / 14:21
1

Try a function

function IntToDate(i){
    var d = new Date(i);
    return moment(d.toUTCString());
}

IntToDate(1424102400000).format('DDMMYYYY');

I quickly looked at the doc of momentjs , whether it is possible to change to pass the int directly without needing the intermediate date

    
22.07.2015 / 14:21