Convert Date format YYYY-MM-DDThh:mm - javascript

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

Related

Get Hour from Date String in Timezone of Date String, Not Converted to Current Time

I have date strings like Sun May 10 2020 13:00:00 GMT-0800 (Pacific Standard Time) and 2020/05/10 13:00:00. I would like to retrieve the hour from these string in 24 hour format (in this case it would be, 13).
I have tried something like new Date('Sun May 10 2020 13:00:00 GMT-0800 (Pacific Standard Time)').getHours() but that first converts the hour to system's current timezone and then returns the hour. Instead, I need to be able to retrieve the literal hour from variously formatted date strings such as the two above examples.
Edit: Obviously the above is not the correct approach, what I'm asking is, is there a universal way to parse the hour out of a date string, whether it be the above two examples or others, and without regex?

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

Javascript convert date string to date and time

I have a date string in the below format,
Wed Feb 21 2018 09:20:46 GMT+0530 (IST)
using Javascript want to convert the date string like this, i.e, date and time separated with 'T'.
2018-02-21T09:20:46.66
You can use Moment.js
moment("Wed Feb 21 2018 09:20:46 GMT+0530 (IST)").format('YYYY-MM-DDTHH:MM:SS')
This Code will do it.
var date = new Date()
date.toJSON().slice(0,10).replace(/-/g,'-') + "T" +
date.toJSON().split('T')[1].split('.')[0]
It looks like you need a Date String to be converted to ISO Format. You can use the toISOString() method as follows:
var event = new Date('Wed Feb 21 2018 09:20:46 GMT+0530 (IST)');
console.log(event.toISOString());
Result is "2018-02-21T03:50:46.000Z"
You can use .slice() method to trim the last Z if you do not require it. You'd then have to use event.toISOString().slice(0,-1) to trim the last character in the returned ISO String.

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?

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.

Categories