This question already has answers here:
Parsing a string to a date in JavaScript
(35 answers)
Closed 3 years ago.
I have Fullcalender's events which are return date as String format it's like this 'Wed Oct 23 2019 00:00:00 GMT+0530 (India Standard Time)' I want to convert this to javascript date exactly the same format Wed Oct 23 2019 00:00:00 GMT+0530 (India Standard Time).because I want to use this method toDateString() to check some conditions.
From a string to Date type :
const dateStr = 'Wed Oct 23 2019 00:00:00 GMT+0530 (India Standard Time)';
const dateObj = new Date(dateStr) // insert your date variable here
Related
I have the (german) Output: Fri Sep 09 2022 02:00:00 GMT+0200 (Mitteleuropäische Sommerzeit)
and I would like to have just the yyyy-mm-dd date: 2022-09-09
Thank you for the help!
Date's are not too great in JS, the reason that libs like moment.js etc are very popular.
But one idea is to simply replace the GMT+0300 to GMT+0000, and then parse into Date object and use ToISOString and slice the first 10 chars.
eg.
function shortDate(dt) {
return new Date(dt.replace(/GMT\+..../,'GMT+0000')).toISOString().slice(0, 10);
}
console.log(shortDate('Fri Sep 09 2022 02:00:00 GMT+0200 (Mitteleuropäische Sommerzeit)'));
I have a variable dateSubmitted having the value dateSubmitted": "07-09-20:11:03:30
I am using Angular Version 7
I am using the date filter within HTML to format the date as below:
<td> {{element.dateSubmitted | date:'MMMM d, y'}} </td>
The date value is correct but the year is incorrectly showing as 2001 in the output July 9, 2001
The correct output is July 9, 2020
See DatePipe input value
value – The date expression:
a Date object
a number (milliseconds since UTC epoch)
an ISO string (https://www.w3.org/TR/NOTE-datetime).
Your date does not conform to ISO string specification, in particular, it has year at the end. The parser seems to interpret is as an hour.
Compare:
new Date('07-09:11:03:30')
// Sun Jul 01 2001 09:11:03 GMT+0200 (Central European Summer Time)
new Date('07-09-:11:03:30')
// Mon Jul 09 2001 11:03:30 GMT+0200 (Central European Summer Time)
new Date('07-09-20:11:03:30')
// Mon Jul 09 2001 20:11:03 GMT+0200 (Central European Summer Time)
I am generating and sending full date string from javascript Date() function which returns full string date format like this:
Sun Jan 01 2017 00:00:00 GMT+0100 (Central European Standard Time)
Carbon parser wont accept this format for creating the same date on server side. This does not work:
$date = Carbon::parse('Sun Jan 01 2017 00:00:00 GMT+0100 (Central European Standard Time)');
Error Failed to parse time string (Sun Jan 01 2017 00:00:00 GMT+0100 (Central European Standard Time)) at position 41 (l): Double timezone specification
If I remove (Central European Standard Time) works:
$date = Carbon::parse('Sun Jan 01 2017 00:00:00 GMT+0100');
Then it correctly creates date.
Can JS default Date() be used in Carbon somehow or will I have to format date before sending it to Carbon?
Carbon extends PHP's native DateTime class, so you can use createFromFormat instead:
$date = 'Sun Jan 01 2017 00:00:00 GMT+0100 (Central European Standard Time)';
$carbon = Carbon::createFromFormat('D M d Y H:i:s e+', $date);
The important part of the format specification is the + at the end, which tells it to ignore any trailing data.
See https://3v4l.org/Rnen7 for a demo (using DateTime rather than Carbon)
You can pass the date in ISO format, Carbon understands ISO format. You can get the date in ISO format using new Date().toISOString()
My date format input is like -Mon Nov 23 2015 10:24:04 GMT+0530 (India Standard Time)
I want validate above date with regularExpression like if the user given input formatt is Mon Nov 23+-}\ 2015 10:24:04 abcd GMT+0530 (India Standard Time) then validate that date with RegEx
Why are the two expressions below returning different results?
Date().valueOf()
"Fri Feb 07 2014 16:03:34 GMT-0500 (Eastern Standard Time)"
new Date().valueOf()
1391807020802
Date().toString()
"Fri Feb 07 2014 16:09:21 GMT-0500 (Eastern Standard Time)"
new Date().toString()
"Fri Feb 07 2014 16:09:26 GMT-0500 (Eastern Standard Time)"
Date() returns a timestamp formatted as a string.
new Date() returns a Date instance.
Instances of the Date constructor have values that convert to numbers, which is why new Date().valueOf() returns a number. Strings are simply strings, so when you call Date().valueOf() you get the same string result.