Javascript Date

Asked

Viewed 233 times

3

I have a date in the following format: 1424102400000 , that is, an integer value. (I don’t know how to say the name of this format).
I need to convert it to a value presentable to the user, so far so good. I’m using the method toUTCString() and I can also use the momentjs. So the value shown above is the same as Mon, 16 Feb 2015 16:00:00 GMT.
The issue is that such value presented to the user can be changed and then I need to convert it again to an integer value similar to the pattern I initially presented (of course the value will be different if the user has made any changes).
Can someone help me?
Look at that Fiddle, values do not match!

  • http://jsfiddle.net/tp1dgjgv/

  • change to valueOf() just as I said, the difference is that one is 1000 times bigger, if Voce uses Unix() and multiply by *1000 is pretty much the same thing

  • this format seems to be timestamp, the problem happens because of the milliseconds, set the value of seconds and milliseconds to 0 in the input and output;

2 answers

3


If I got it right you want to transform from format UNIX TimeStamp to a display format and then be able to 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() equals Unix() * 1000 as it displays in milliseconds

Documentation momentJS: http://momentjs.com/docs/#/Displaying/Unix-offset/

Unix Time Stamp

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

Correction of your fiddle: http://jsfiddle.net/tp1dgjgv/

  • You could make an example fiddle to be clearer with the value I put? 1424102400000 Turn it into a date, view it, convert to Unix, view it and finally turn it into date again to compare the obtained dates, please... Mine is not beating.

  • unfortunately I can’t create a Jsfiddle for you now, but check out the Moment documentation, test your code using console.log() while running and see what outputs each command

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’s momentjs, whether it is possible to change to pass the direct int without the intermediary’s need date

Browser other questions tagged

You are not signed in. Login or sign up in order to post.