JavaScript Date() differs when timezone changes

vivek vijayakumar

I need to convert my date to mm-dd-yyyy format. So I used a method like this:

var dt=new Date(2016-06-21);

var ddte='';

ddte=(("0" + (dt.getMonth() + 1)).slice(-2))+"-"+(("0" + dt.getDate()).slice(-2))+"-"+dt.getFullYear();

It works fine in my local timezone (GMT+05:30). But when I change my timezone to GMT -5:00, it gives the wrong result: 06-20-2016. The result I want is 06-21-2016.

  1. Can anyone please explain the problem?

  2. How can I get the correct result?

  3. Is it a bug?

Johan Karlsson

Your date passed to Date() constructor will be treated as UTC time zone. Getting the time with Date.getMonth() will get your local time zone. You're probably looking for Date.getUTCMonth().

var dt=new Date("2016-06-21");

var ddte='';

ddte=(("0" + (dt.getUTCMonth() + 1)).slice(-2))+"-"+(("0" + dt.getUTCDate()).slice(-2))+"-"+dt.getUTCFullYear();

console.log(ddte);

Though in this case I see no use for using Date at all; this should suffice:

var parsedDate = "2016-06-21".replace(/(\d{4})-(\d{2})-(\d{2})/, "$2-$3-$1");
console.log(parsedDate);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

timestamp to date changes date timezone

From Dev

Angular JS - Date changes when submitting to $http - Timezone issue

From Dev

javascript Date timezone issue

From Dev

Javascript Date() timezone incosistency

From Dev

How to preserve timezone offset when posting a JavaScript date object?

From Java

Convert date to another timezone in JavaScript

From Java

Parse date without timezone javascript

From Dev

Javascript, Date.parse with no timezone

From Dev

Carbon, Javascript Date Objects and timezone

From Dev

Javascript date convert to timezone with format

From Dev

Rails timezone changes when increasing timestamp

From Dev

Adding lubridate dates to vector changes timezone and thus date

From Dev

Date Issue when change Timezone to London

From Dev

Convert unix timestamp with a timezone to javascript date

From Dev

Convert postgres timestamp (+ timezone) to Date object [Javascript]

From Java

How to JSON stringify a javascript Date and preserve timezone

From Dev

remove the local timezone from a date in javascript?

From Dev

Parse javascript date with consistent timezone interpretation?

From Dev

How to format a date in a given timezone in Javascript?

From Dev

Convert postgres timestamp (+ timezone) to Date object [Javascript]

From Dev

JSON date with Timezone offset not converting to JavaScript Date correctly

From Dev

Setting Minutes in Calendar differs depending on TimeZone

From Dev

Why does the `-` (minus) interpretation of GNU date differs from the intuitive one, when a date is specified?

From Dev

Why does the `-` (minus) interpretation of GNU date differs from the intuitive one, when a date is specified?

From Dev

TimeZone changes Java and Database

From Dev

Timezone changes do not persist

From Dev

Change data in repeater when date changes in .datepicker

From Dev

Adding missing rows but not when date changes

From Dev

php calendar show wrong date when add timezone

Related Related

  1. 1

    timestamp to date changes date timezone

  2. 2

    Angular JS - Date changes when submitting to $http - Timezone issue

  3. 3

    javascript Date timezone issue

  4. 4

    Javascript Date() timezone incosistency

  5. 5

    How to preserve timezone offset when posting a JavaScript date object?

  6. 6

    Convert date to another timezone in JavaScript

  7. 7

    Parse date without timezone javascript

  8. 8

    Javascript, Date.parse with no timezone

  9. 9

    Carbon, Javascript Date Objects and timezone

  10. 10

    Javascript date convert to timezone with format

  11. 11

    Rails timezone changes when increasing timestamp

  12. 12

    Adding lubridate dates to vector changes timezone and thus date

  13. 13

    Date Issue when change Timezone to London

  14. 14

    Convert unix timestamp with a timezone to javascript date

  15. 15

    Convert postgres timestamp (+ timezone) to Date object [Javascript]

  16. 16

    How to JSON stringify a javascript Date and preserve timezone

  17. 17

    remove the local timezone from a date in javascript?

  18. 18

    Parse javascript date with consistent timezone interpretation?

  19. 19

    How to format a date in a given timezone in Javascript?

  20. 20

    Convert postgres timestamp (+ timezone) to Date object [Javascript]

  21. 21

    JSON date with Timezone offset not converting to JavaScript Date correctly

  22. 22

    Setting Minutes in Calendar differs depending on TimeZone

  23. 23

    Why does the `-` (minus) interpretation of GNU date differs from the intuitive one, when a date is specified?

  24. 24

    Why does the `-` (minus) interpretation of GNU date differs from the intuitive one, when a date is specified?

  25. 25

    TimeZone changes Java and Database

  26. 26

    Timezone changes do not persist

  27. 27

    Change data in repeater when date changes in .datepicker

  28. 28

    Adding missing rows but not when date changes

  29. 29

    php calendar show wrong date when add timezone

HotTag

Archive