I have a Date format like this "Fri Apr 20 2020 00:00:00 GMT+0530 (India Standard Time)"
I want to convert that above format to this format 2020-04-20T00:00:00.000Z
Actually I tried this JSON.stringify(new Date("Fri Apr 20 2020 00:00:00 GMT+0530 (India Standard Time)")) while using this am getting the output one day before 2020-04-19T18:30:00.000Z
so please anyone help me to convert this date format "Fri Apr 20 2020 00:00:00 GMT+0530 (India Standard Time)" like this 2020-04-20T00:00:00.000Z
Thanks in Advance.
Your date seems to be a standard string representation of new Date(), you can get the desired format by using new Date().toISOString()
console.log(new Date().toString())
console.log(new Date().toISOString())
// To create it from string
const dateStr = "Fri Apr 20 2020 00:00:00 GMT+0530 (India Standard Time)"
console.log(new Date(dateStr).toISOString())
Anurag Srivastava's answer shows how you should parse the string and format it in the required format (given that the string is in one of the two formats supported by ECMA-262 and considering Why does Date.parse give incorrect results?).
Note that "Fri Apr 20 2020 00:00:00 GMT+0530 (India Standard Time)" is the same instant in time as "2020-04-19T18:30:00.000Z". The first string is offset from UTC by 5 hr 30 min, so the equivalent UTC time is 5 hr 30 min earlier, which means the date is the previous day.
You haven't given a reason why you want to treat it as UTC and not consider the offset, so I don't think you should.
However, if you do have a good reason to parse it as UTC and ignore the supplied offset, then you can either:
Modify the input string to set the offset as +0 and parse it using the built–in parser
Parse the string yourself and treat it as UTC
let s = "Fri Apr 20 2020 00:00:00 GMT+0530 (India Standard Time)";
// #1 Modify the input string, setting the offset to +0
let d = new Date(s.replace(/GMT.*$/,'GMT+0000')).toISOString();
console.log(d.toISOString());
// #2 Bespoke parser
function parseAsUTC(s) {
let months = ['jan','feb','mar','apr','may','jun',
'jul','aug','sep','oct','nov','dec'];
let b = s.split(/\W/);
return new Date(Date.UTC(b[3], months.indexOf(b[1].toLowerCase()),
b[2], b[4], b[5], b[6]));
}
console.log(parseAsUTC(s).toISOString());
How can I get Fri Oct 25 2019 from a date object as below?
Fri Oct 25 2019 15:27:01 GMT+0530 (India Standard Time)
Use the toDateString() function from JavaScript.
let d = new Date();
// note : the actual display output depend on your browser/system settings (locale)
console.log(d.toString());
console.log(d.toDateString());
This will take the first "date only" part of the string that is displayed from the date object.
It could also be done with string manipulation, but this function is actually intended for this usecase, so probably clearer.
Doc for toDateString function
How to get Date Format as
Fri Dec 03 2014 00:00:00 GMT+0530 (IST)
Todays date with time 00:00:00 in javascript ?
Any help appreciated
var myDate = new Date();
myDate.setHours(0,0,0);
alert(myDate);
jsfiddle http://jsfiddle.net/nrud7j8v/2
about javascript setHours() method http://www.w3schools.com/jsref/jsref_setHours...
Retrieved DateTextBox value looks like:
Thu Jan 01 1970 17:17:33 GMT+0100 (W. Europe Standard Time)
What is the best way to convert this into UNIX timestamp?
var unixtime = Date.parse("24-Nov-2009 17:57:35").getTime()/1000
I have a string:
"2014-07-30T21:11:35.6300000"
but when I do
new Date(str)
It computes:
Wed Jul 30 2014 17:11:35 GMT-0400 (Eastern Daylight Time)
Q: How do I tell JavaScript not to calculate GMT -0400?
(new Date("2014-07-30T21:11:35.6300000")).toUTCString()