Creating UTC time in momentjs is returning weird results

GETah

I am tearing my hear off on a Monday evening! Can anyone please help me understand the problem below? I have the following string on my browser 01-09-2014 (this is in local time), I want to convert this into UTC so that I can save that back to the server. Here is what I am doing:

    var localDate = moment("01-09-2014", "DD-MM-YYYY");
    var utcDate = localDate.utc();
    console.log("Local Date " + localDate.toDate() + " UTC Date " + utcDate.toDate());

Strangely enough, the last line outputs:

Local Date Mon Sep 01 2014 00:00:00 GMT+0200 (Romance Daylight Time) UTC Date Mon Sep 01 2014 00:00:00 GMT+0200 (Romance Daylight Time) 

QUESTION

  • Why does the UTC date look exactly the same as the local one (I expect the local time to be 2 hours ahead of UTC given I am in an GMT+2 zone) ?
  • Why does the UTC date have GMT+0200? I expect a UTC date to have GMT+000

Could you please help me with these two questions?

conceptdeluxe

Thoug I am not sure about ... but I think it should be:

var utcDate = localDate.utc().format()

See:

http://momentjs.com/docs/#/parsing/utc/

UPDATED

Fiddle:

http://jsfiddle.net/qhk9tnLr/2/

JS:

console.log("Local Date: " + moment("01-09-2014", "DD-MM-YYYY").toDate());
console.log("UTC Date: " + moment("01-09-2014", "DD-MM-YYYY").utc().format("ddd MMM DD YYYY HH:mm:ss zZZ"));

Output:

Local Date: Mon Sep 01 2014 00:00:00 GMT+0200
UTC Date: Sun Aug 31 2014 22:00:00 UTC+0000

Conclusion:

toDate() will give the local time - to get UTC use utc().format()

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Time produces weird results with leading sudo call

분류에서Dev

MomentJS 및 UTC 날짜 처리

분류에서Dev

MomentJS 및 UTC 날짜 처리

분류에서Dev

rsync with Cygwin creating weird directories

분류에서Dev

Change the time to UTC format

분류에서Dev

Page not returning results

분류에서Dev

Weird bash results using cut

분류에서Dev

Weird issue with returning empty struct instead of NULL

분류에서Dev

converting time from UTC to CST

분류에서Dev

Why is Scrapy returning duplicate results?

분류에서Dev

ViewPager Cache is making SearchView results appear weird

분류에서Dev

momentjs를 사용하여 Epoch UTC를 String EST로 변환

분류에서Dev

momentjs를 사용하여 Epoch UTC를 String EST로 변환

분류에서Dev

UTC (탄력적 검색)를 MomentJs 현지 시간으로

분류에서Dev

python: UTC to local daylight savings time

분류에서Dev

Is there any way to display time in UTC on the panel?

분류에서Dev

Weird now() time difference with Postgres triggers

분류에서Dev

MySQL Full Text Search Returning 0 Results

분류에서Dev

mysqli queries not returning results inside function

분류에서Dev

scraper only returning results for first 2 inputs

분류에서Dev

ElasticSearch Ruby returning results without ElasticSearch running

분류에서Dev

nslookup returning fake results for one particular domain

분류에서Dev

Internet category not returning any results in Synapse

분류에서Dev

Returning Valid Time Slots with a Given Interval

분류에서Dev

/dev/urandom returning same value every time

분류에서Dev

ColdFusion UTC TIME LTE 검사와 비 UTC 시간

분류에서Dev

is time() returning same value for all locations (time zones)?

분류에서Dev

Local Time & UTC Confusion C#, SQL Server

분류에서Dev

linux redhat + change time from UTC to GMT+X

Related 관련 기사

뜨겁다태그

보관