This question already has answers here:
Javascript - reformat date string to ISO8601
(2 answers)
Closed 3 years ago.
How can i format date in JS given in this format: Mon Jan 08 1996 00:00:00 GMT+0100 to string like this: "1996-01-06T23:00:00.000Z" ?
var str = "Mon Jan 08 1996 00:00:00 GMT+0100";
var date = new Date(str);
console.log(date.toISOString());
Make the string a date object, then convert it to ISO format. That's all there is.
use the following :
var event = new Date('Mon Jan 08 1996 00:00:00 GMT+0100');
console.log(event.toISOString());
First convert Mon Jan 08 1996 00:00:00 GMT+0100 to Date using new Date() function.
Second convert the new date into ISO format using toISO() function.
Related
This question already has answers here:
Javascript date format like ISO but local
(12 answers)
How do I output an ISO 8601 formatted string in JavaScript?
(16 answers)
Closed 4 months ago.
I have a date like this
Sat Oct 29 2022 02:00:00 GMT-0600 (Mountain Daylight Time)
I am running this function on this date
const date = Sat Oct 29 2022 02:00:00 GMT-0600 (Mountain Daylight Time)
console.log(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds())
Which returns
2022 9 29 2 0 0
Is it possible to convert these numbers to
2022-10-29T02:00:00.000
the whole point of this is to use a library like moment-timezone and parse 2022-10-29T02:00:00.000 to a new timezone for example new_york. Im doing this to get the UTC time for 2022-10-29T02:00:00.000 in new york. The issue with new Date(2022-10-29T02:00:00.000) is that it always converts it to my local timezone.
This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 3 years ago.
I am looking for a date output like : "2019-05-29T06:33:33.537+05:30" - which includes gmt value. I tried to get the same by using:
var newFormat = new Date();
console.log(newFormat.toISOString());
But getting value as : 2019-06-16T03:00:27.989Z -not finds the GMT value added. What is the correct approach to get the required value?
Thanks in advance.
You can use toString():
var newFormat = new Date();
console.log(newFormat.toString());
toString gives you Wed Jan 23 2019 17:23:42 GMT+0800 (Singapore
Standard Time).
toDateString gives you Wed Jan 23 2019.
toLocaleString gives you 23/01/2019, 17:23:42.
toLocaleDateString gives you 23/01/2019.
toGMTString gives you Wed, 23 Jan 2019 09:23:42 GMT.
toUTCString gives you Wed, 23 Jan 2019 09:23:42 GMT.
toISOString gives you 2019-01-23T09:23:42.079Z.
This question already has an answer here:
Initialize JS date object from HTML date picker: incorrect date returned
(1 answer)
Closed 3 years ago.
When I take in 3/15/2019 as the input date and then read back the time with console.log(fromDate) I get Thu Mar 14 2019 19:00:00 GMT-0500 (Central Daylight Time).
Why is it not Fri Mar 15 2019 00:00:00 ?
Is it because of the time on my local computer?
Is there a way to achieve the desired output?
fromDate = new Date(document.getElementById("fromDate").value);
This happens because new Date consider the date as UTC, so
var date = new Date('3/15/2019')
creates the date Fri Mar 15 2019 00:00:00. When you print the string however it considers your actual timezone, which is GTM-0500, so it removes 5 hours from the original date, yielding Thu Mar 14 2019 19:00:00.
This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 4 years ago.
From service I get date string in format 'yyyy-mm-dd' (eb, '2017-09-14')
In JavaScript, I convert this string to Date like as mentioned below:
new Date('2017-09-14')
It works just fine. But I am facing problem in different time zones. For example
In EST Timezone: Wed Sep 13 2017 20:00:00 GMT-0400 (Eastern Daylight Time) and
In IST Timezone: Thu Sep 14 2017 05:30:00 GMT+0530 (India Standard Time)
Is there a way to convert it to the same date e.g. Sep 14, 2017
YOu can do it like this
var d=new Date(date value);
var actualDate=d.getDate();
var actualMonth=d.getMonth()+1;
var actualYear=d.getFullYear();
var formattedDate=actualDate + '/' + actualMonth + '/' + actualYear;
You can save it to any format
I'm working with an API which expects json date format.
I need to convert my javascript date
Sat Jan 17 1970 07:28:19 GMT+0100 (Romance Standard Time)
To json date format
/Date(1405699200)/
Is anything of the following ok?
console.log(new Date('Sat Jan 17 1970 07:28:19 GMT+0100 (Romance Standard Time)').toJSON());
console.log(new Date('Sat Jan 17 1970 07:28:19 GMT+0100 (Romance Standard Time)').valueOf());
/Date(1405699200)/
Here 1405699200 is your date in epoch format.
You can do this :
var myDate = new Date("July 1, 1978 02:30:00"); // Your timezone!
var myEpoch = myDate.getTime()/1000.0;
document.write(myEpoch);
To convert human readable date format to Epoch format : https://www.epochconverter.com/programming/#javascript