javascript - Date class converts Timestamp wrong -


in application, creating live console output messages timestamp , contents. read, approach using below date() class should work expected, timestamp multiplied 1000 milliseconds.

i logging timestamp debugging purposes , getting values "1441041070066". when plug these epoch/unix converters, date/time correct. code giving nonsense "22:7:46" , 1 minute later "20:48:37". can please explain doing wrong in case?

messages.foreach( function (item) {     var timestamp = item.timestamp; // /date(1440823073243)/     var timestamp = timestamp.substring(timestamp.lastindexof("(")+1, timestamp.lastindexof(")"));      console.log(timestamp);      var source = item.source;     var type = item.type;     var contents = item.contents;      // date/time in milliseconds     var date = new date(timestamp * 1000);     var time = date.gethours() + ":" + date.getminutes() + ":" + date.getseconds();      console_log("<font color='blue'>" + time + "</font>" + ": " + contents); }); 

the timestamp you've got in milliseconds. don't know converter used, if put 1440823073243 epochconverter.com shows:

assuming timestamp in milliseconds

... , comes timestamp of gmt: sat, 29 aug 2015 04:37:53 gmt.

so should remove * 1000 part of code, parse timestamp (which still string) number:

var date = new date(parseint(timestamp)); 

additionally, should use alternative ways of formatting date:

  • you're using users's time zone; it's not clear whether that's want or not. (it may be, should think it.)
  • by using string concatenation, won't padding, leading strings "22:7:46".

basically, research alternative formatting options - whether part of javascript standard libraries, or moment.js.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -