简体   繁体   中英

Getting moment.js moment 0 in unix timestamp

I'm trying to use moment.js for similar calculations as python timedelta.

what i have trouble with is this:

var d = moment(0);
d
Moment {_i: 0, _f: undefined, _l: undefined, _isUTC: false, _d: Thu Jan 01 1970 02:00:00 GMT+0200 (EET)…}
_d: Thu Jan 01 1970 02:00:00 GMT+0200 (EET)
_f: undefined
_i: 0
_isUTC: false
_l: undefined
__proto__: Object
d.hours();
2
d.days();
4

How can i get moment timestamp equal to unix timestamp 0, cause moment(0) does not seem to give me moment where days, hours, seconds and whatnot is all 0 and which transforms to unix timestamp 0 with moment.js format 'X' ( link )

In response to comments: Usually timepickers work for picking time of certain date/day. And the value of timepicker 14:35 is usually interpreted as 14 hours and 35 minutes. You COULD use the same picker for letting user choose PERIOD (or timedelta) not TIME. Which would mean that there is 14 hours and 35 minutes between something, or something takes that time... so on...

And you can use moment.js for simulating that functionality. Because:

moment(0).utc().hours == 0
moment(0).utc().minutes == 0
moment(0).utc().seconds == 0

You can just add (or let user add) hours, minutes, seconds to choose not time but PERIOD. Or timedelta. Unfortunatley this fails, when period is longer than 24 hours. because moment(0).utc().days() equals to 4 not 0.

Not sure why this WHY is relevant, when for all reasons i can think of moment(0).utc().days() should be 0 not 4...

I think your running into issues with the time zone that moment detects you in. If you do

moment(0).toISOString() 

You should see 0s for all days etc. You can set the time zone to UTC by doing something like

var a = moment(0);
a.utc();
a.toString()

a.toString should now display as the correct timezone.

Also, moment(0).utc().days() is "Wednesday" (Or whatever day currently is).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM