admin管理员组

文章数量:1420213

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 ments: 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'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 ments: 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...

Share Improve this question edited Nov 11, 2013 at 15:15 Odif Yltsaeb asked Nov 11, 2013 at 14:58 Odif YltsaebOdif Yltsaeb 5,67614 gold badges54 silver badges81 bronze badges 8
  • var d = moment(0).utc(); I think you need to tell moment that this "timestamp" is in UTC. – gen_Eric Commented Nov 11, 2013 at 15:01
  • Also, why exactly are you doing moment(0)? – gen_Eric Commented Nov 11, 2013 at 15:04
  • moment(0).utc().days(); still prints out 4. I explained why in question. I want to present time to user as timedelta (difference between 2 timestamps) not as certain timestamp. Python datetime.timedelta is excellent for it. I thought that i could simulate same functionality with moment.js, but so far it seems, that i cant get it working like this. – Odif Yltsaeb Commented Nov 11, 2013 at 15:08
  • 1 I know nothing about timedelta, but what 2 timestamps are you trying to get the difference between? Why do you think you need moment(0) for this? Does this not work for you: momentjs./docs/#/displaying/difference? Can you show us the actual problem you are having? I think this might be an XY problem. – gen_Eric Commented Nov 11, 2013 at 15:15
  • 1 I figured out why days() is 4 (ok, @jeremy did). That's the DAY OF THE WEEK! You wanted d.date() for the day of the month (which would actually be 1, not 0) momentjs./docs/#/get-set/date ;-) – gen_Eric Commented Nov 11, 2013 at 15:23
 |  Show 3 more ments

1 Answer 1

Reset to default 3

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).

本文标签: javascriptGetting momentjs moment 0 in unix timestampStack Overflow