时间戳记是时间点。通常,这可以通过经过epoc(1970年1月1日,美国中部时间UTC的Unix
Epoc)后的毫秒数来表示。该时间点的格式取决于时区。虽然是同一时间点,但各个时区之间的“小时值”并不相同,因此必须考虑到与UTC的时差。
这是一些代码来说明。重要的是时间是通过三种不同的方式捕获的。
var moment = require( 'moment' );var localDate = new Date();var localMoment = moment();var utcMoment = moment.utc();var utcDate = new Date( utcMoment.format() );//These are all the sameconsole.log( 'localData unix = ' + localDate.valueOf() );console.log( 'localMoment unix = ' + localMoment.valueOf() );console.log( 'utcMoment unix = ' + utcMoment.valueOf() );//These formats are differentconsole.log( 'localDate = ' + localDate );console.log( 'localMoment string = ' + localMoment.format() );console.log( 'utcMoment string = ' + utcMoment.format() );console.log( 'utcDate = ' + utcDate );//One to show conversionconsole.log( 'localDate as UTC format = ' + moment.utc( localDate ).format() );console.log( 'localDate as UTC unix = ' + moment.utc( localDate ).valueOf() );
哪个输出:
localData unix = 1415806206570localMoment unix = 1415806206570utcMoment unix = 1415806206570localDate = Wed Nov 12 2014 10:30:06 GMT-0500 (EST)localMoment string = 2014-11-12T10:30:06-05:00utcMoment string = 2014-11-12T15:30:06+00:00utcDate = Wed Nov 12 2014 10:30:06 GMT-0500 (EST)localDate as UTC format = 2014-11-12T15:30:06+00:00localDate as UTC unix = 1415806206570
以毫秒为单位,每个都相同。它是完全相同的时间点(尽管在某些运行中,后一毫秒要高一点)。
就格式而言,每个都可以在特定时区中表示。而且,该时区字符串的格式看起来完全不同,而且时间完全相同!
您要比较这些时间值吗?只需转换为毫秒。一个毫秒值始终小于,等于或大于另一个毫秒值。
您是否要比较特定的“小时”或“天”值,并担心它们“来自”不同时区?首先使用转换为UTC
moment.utc( existingDate),然后进行操作。从数据库中出来时,这些转换
console.log的示例是该示例中的最后一次调用。



