0

I need date format like : 2018-10-04T20:35:28. in javascript. I don't know what format is this, but I already try follow

Now I have this:

var now = new Date();
var isoDate = new Date(now).toISOString();

My output is:

2018-10-05T04:55:58.896Z

But I have a wrong day because actual date is:

Thu 4 Oct 2018 22:56:53 CST

Why i have +1 day in all dates.

Kishan Oza
  • 1,707
  • 1
  • 16
  • 38
Brygom
  • 818
  • 1
  • 12
  • 31

2 Answers2

0

like @Nisarg Shah said "The ISO string is in UTC, the one in console is in your local time zone" . You can change it using this

new Date().toLocaleString("en-US", {timeZone: "America/New_York"})

Check this out for more information.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString

Shadow
  • 8,749
  • 4
  • 47
  • 57
akshay kishore
  • 1,017
  • 8
  • 16
  • The OP wants an ISO 8601 format date in the local timezone and without timezone. This doesn't really help, since *toLocaleString* creates an implementation dependent string that is typically not ISO 8601. – RobG Oct 05 '18 at 06:19
  • There are 6 time zones in US I learned. So a Website that needs to run for American audience, what is the solution? America/New_York will give me new york time zone, so does it mean, If I see the website from san fransico, I will see wrong timing? – BILAL MALIK Oct 05 '18 at 06:39
  • @BILALMALIK— if "wrong" means not in their local timezone, then yes. If you want to format a date using host timezone settings, then format the string as a local timestamp. There are many questions here already on how to do that. – RobG Oct 05 '18 at 07:08
  • @RobG Can you clarify me little more on this? Host time zone = where my server is hosted- say it is in Brazil, And I'm browsing from San Francisco with wrong manual timing on the local machine. So are you saying solution = Brazil +- San Francisco time or San Francisco local machine time, or what? Please explain. – BILAL MALIK Oct 05 '18 at 07:15
  • 1
    @BILALMALIK—javascript requires a host to provide the environment for the code to run it, so "host" is the system on which the implementation is running. If your code is running in a browser on a laptop, that's the host. If you use "local" date methods, you'll get the timezone offset for the host system (whatever that might be). – RobG Oct 05 '18 at 08:22
  • @RobG - yes I get the point we use the local date method to show the client its local time. Thanks for teaching me "host"? , javascript client side host, website host confuses me so much. Thanks Rob – BILAL MALIK Oct 05 '18 at 09:15
0
var isoDate = new Date(now).toISOString();

// Output
2018-10-05T04:55:58.896Z

This isoDate is in UTC. You can see that there is a 'Z' in the end of the string. This means that the date is in UTC.

You can use Moment Timezone (moment.js) to convert any given date to another timezone.

moment.tz('2018-10-05T04:55:58.896Z', 'America/Toronto').format(); 

Just change the timezone name to the one you want to convert.

For Further Details https://momentjs.com/timezone/docs/#/using-timezones/

Dilushika Weerawardhana
  • 1,441
  • 1
  • 12
  • 16