Convert date MongoDB / Javascript, but Nan - javascript

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.

Related

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)

javascript full text Date() format with PHP carbon

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

Convert Date format YYYY-MM-DDThh:mm

I request a API, this bellow format is not accept:
Thu Apr 12 2018 00:00:00 GMT+0800 (CST)
the
"start_time":["Date format error. Please ues this type format:YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z]。"
part of my request:
Part of my request data:
Content-Disposition: form-data; name="start_time"
Thu Apr 12 2018 00:00:00 GMT+0800 (CST)
------WebKitFormBoundaryPtZBgjhBEahHBOLY
Content-Disposition: form-data; name="end_time"
Thu May 31 2018 00:00:00 GMT+0800 (CST)
I have two questions:
Does the Thu Apr 12 2018 00:00:00 GMT+0800 (CST) is one of the Date format? And then there is how many types of Date format?
How can I convert the example Date to this format YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z]?
You don't say how you get the string. If you have a Date object, you can use toISOString to generate a timestamp in the required format. To answer your questions:
Does the Thu Apr 12 2018 00:00:00 GMT+0800 (CST) is one of the Date format?
No. The ECMAScript Date object is only required to parse the subset of ISO 8601 noted in the language specification, ECMA-262. Support for any other format is implementation dependent.
In general, you shouldn't rely on the built-in parser. It's much better to either write your own parser, or if you need to support a number of formats, use a library. There are plenty of good ones to choose from, but the bottom line is that you must know the timestamp format in order to be sure of correctly parsing it, whether you use a library or write your own parser.
And then there is how many types of Date format?
A Date object doesn't have any format, it is simply a time value that represents a moment in time in UTC plus some fairly simplistic methods. The format of toString is implementation dependent, however toISOString is compliant with ISO 8601. But to use it requires a Date object, so you'd have to parse the string to a Date to use it.
How can I convert the example Date to this format YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z]?
You can either parse it to a Date (using your own parser or a library) then use toISOString (or library equivalent), or just reformat the string, e.g.
var s = 'Thu Apr 12 2018 00:00:00 GMT+0800 (CST)';
function toISOString(s) {
var months = {jan:'01',feb:'02',mar:'03',apr:'04',may:'05',jun:'06',
jul:'07',aug:'08',sep:'09',oct:'10',nov:'11',dec:'12'};
var b = s.split(' ');
return b[3] + '-' +
months[b[1].toLowerCase()] + '-' +
('0' + b[2]).slice(-2) + 'T' +
b[4] + b[5].substr(3);
}
console.log(toISOString(s));

MomentJS ISO date formatting

Date before ISO conversion it was like 'Sat Oct 22 2016 00:18:38 GMT+0530 (IST)' But after convert using moment i.e. moment().toISOString() It gives
result output like '2016-10-21T18:48:00.337Z', here it gives date 21.
why?

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