javascript full text Date() format with PHP carbon - javascript

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()

Related

JavaScript converting long date to short date

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)'));

Date Filter incorrectly formatting the date value

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)

Convert date MongoDB / Javascript, but Nan

I have a little problem with a date conversion in javascript.
I have a date format like this one Wed Mar 09 10:32:14 CET 2016 from a Mongo database. When I want to get the month for example, I do this:
d=new Date('Wed Mar 09 10:32:14 CET 2016');
alert(d.getMonth());
But It does not work (NaN in the alert).
I know that a format like Wed Mar 09 2016 10:32:14 GMT will work, but the Database returns me Wed Mar 09 10:32:14 CET 2016.
I want to avoid the date format transformation.
You have to use the format "Mon, 25 Dec 1995 13:30:00 GMT", the Date constructor allow use date as String but with the format specified before, the Date constructor use the Date.parse to parse the string to date, read further details about the Date.parse. Store the date into mongodb in timestamps.

Difference between "new Date().valueOf()" and Date().valueOf()"

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.

How to create Date object from the string date in Javascript for the first years of A.D?

I have next date string:
"Thu Nov 14 0002 01:01:00 GMT+0200 (GTB Standard Time)"
and I'm trying to convert it to the Date object:
date = new Date("Thu Nov 14 0002 01:01:00 GMT+0200 (GTB Standard Time)")
=> Invalid Date {}
and it doesn't work. And
date = new Date("Thu Nov 14 2 01:01:00 GMT+0200 (GTB Standard Time)")
=> Invalid Date {}
doesn't work too
but
date = new Date("Thu Nov 14 2002 01:01:00 GMT+0200 (GTB Standard Time)")
works
Does anyone know an elegant way to parse it ?
You can set any date. including minutes,hours and milliseconds directly using a timestamp-
dates before 1970 are negative integers.
alert(new Date(-62076675540000).toUTCString());
// >> Wed, 13 Nov 0002 23:01:00 GMT
Or you can set the date as a string by replacing the years to make it over 1000,
then subtracting the amount you added with setFullYear()
var d=new Date("Thu Nov 14 1002 01:01:00 GMT+0200 (GTB Standard Time)")
d.setFullYear(d.getFullYear()-1000)
alert(d.toUTCString())
// >> Wed, 13 Nov 0002 23:01:00 GMT
You can automate a conversion to timestamps-
var s="Thu Nov 14 0002 01:01:00 GMT+0200 (GTB Standard Time)";
var y=s.split(' ')[3], y2=5000+(+y);
var d=new Date(s.replace(y,y2));
d.setFullYear(d.getFullYear()-5000)
var timestamp=+d;
alert(timestamp)
// >> -62076675540000
Javascript dates are based on a count of milliseconds since 1 Jan 1970, 00:00:00.000 UTC. Dates before that are not defined.
You'll have to come up with your own way to represent such dates.
edit — well having said that, Javascript seems willing to represent dates with weirdly large negative offfsets from the epoch; offsets that don't fit in 32 bit integers. I suspect that the root cause of your date is simply that the format it's in upsets the parser. There's supposed to be a comma after the day abbreviation.
Another problem (boy this is way more interesting than I thought) is that in Chrome and Firefox at least any year before 100 is treated as an abbreviation for a year in the 20th century.
edit again — according to the Mozilla docs, a Date can be anything in the range of -100,000,000 days before the epoch to 100,000,000 days after it.

Categories