Let's say i have the code below:
var date = moment();
var timestamp = date.unix();
var utc_timestamp = moment.utc().unix();
It is my understanding that if you call utc(). all functions return the value in utc mode.
Well in this case that does not seem the case.
date = {_isAMomentObject: true, _isUTC: false, _locale: r, _d: Thu Mar 24 2016 14:51:35 GMT-0700 (PDT)}
timestamp = 1458856295
utc_timestmap = 1458856295
however. if i use
moment().utc(date.toDate().toUTCString())l
the value seems to be in UTC. but it uses the "current aka NOW value) for the utc value and the moment value.
Question is, how do i get the utc timestamp of a pre existing moment?
i've looked in the suggested questions, and searched SO but i can't seem to find the right solution.
EDIT//
I have server side code generating a timestamp in Golang. This then get's send to my webserver through rest. and converted to a DateTime field in MYSQL.
when i then use moment to generate a timestamp, and query my database for a unix_timestamp(timestamp) between moment1 and moment2 the timestamps from moment are exactly 7 or 8 hours ahead based on DST.
I've converted my DateTime field to just be a timestamp. So far not working but could be another issue in my code.
Let's say i have the code below:
var date = moment();
var timestamp = date.unix();
var utc_timestamp = moment.utc().unix();
It is my understanding that if you call utc(). all functions return the value in utc mode.
Well in this case that does not seem the case.
date = {_isAMomentObject: true, _isUTC: false, _locale: r, _d: Thu Mar 24 2016 14:51:35 GMT-0700 (PDT)}
timestamp = 1458856295
utc_timestmap = 1458856295
however. if i use
moment().utc(date.toDate().toUTCString())l
the value seems to be in UTC. but it uses the "current aka NOW value) for the utc value and the moment value.
Question is, how do i get the utc timestamp of a pre existing moment?
i've looked in the suggested questions, and searched SO but i can't seem to find the right solution.
EDIT//
I have server side code generating a timestamp in Golang. This then get's send to my webserver through rest. and converted to a DateTime field in MYSQL.
when i then use moment to generate a timestamp, and query my database for a unix_timestamp(timestamp) between moment1 and moment2 the timestamps from moment are exactly 7 or 8 hours ahead based on DST.
I've converted my DateTime field to just be a timestamp. So far not working but could be another issue in my code.
Share edited Mar 25, 2016 at 16:24 Puzzle84 asked Mar 24, 2016 at 21:56 Puzzle84Puzzle84 5404 silver badges20 bronze badges1 Answer
Reset to default 6Unix timestamps reflect the number of seconds since the Unix Epoch. They refer to a point on the global timeline. For this reason, all timestamps are UTC. There is no such thing as a local timestamp.
When you use moment in local mode, it is using the JS Date
object to convert the value of a Unix timestamp to local time. The actual Unix timestamp number in the date object remains unchanged.
When you use moment in UTC mode, it is simply displaying the value of the Unix timestamp without making any conversions to the local time.
In either case, the Unix timestamp value reflects the same point on the global timeline.
This is perhaps best illustrated by constructing a moment in both local and UTC mode with the same timestamp. Note that the X
format token denotes that I am using a Unix timestamp in terms of seconds since the Unix epoch. If you pass an integer to the default constructor without specifying this, it assumes you're using a Unix timestamp in terms of milliseconds since Unix epoch.
moment('1458861314', 'X').format()
"2016-03-24T18:15:14-05:00"
The time above is in my local timezone.
moment.utc('1458861314', 'X').format()
"2016-03-24T23:15:14+00:00"
This time is in UTC.
They were both constructed from the same Unix timestamp, and they both refer to the same point on the global timeline.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744931117a4601744.html
评论列表(0条)