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));
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?
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.
I am convert the string to datetime using Date.parse and new Date(). For example if the string is "2/5/2012" then
var date=new Date(Date.parse("2/5/2012")); // Sun Feb 5 00:00:00 PST 2012
var date=new Date(Date.parse("2012/15/3")); //Sun Mar 3 00:00:00 PST 2013
Here my doubt is the string that i am giving takes the below format
"2/5/2012" - "mm/dd/yyyy"
"2012/15/3" - "yyyy/mm/dd"
I Want to know what are the format that i can use to define the date in string.
can i define string like this "3/2012/4"
Thanks in advance
I'm having some trouble with regex expressions while trying to convert a UTC date format 2014-04-07T01:00:00Z to the clients local time.
I am not sure why I cannot format the date with this expression;
function SetDate(sDate) {
var adjusted = sDate.replace(/^([0-9]{4})-([0-9]{2})-([0-9]{2}).([0-9]{2}):([0-9]{2}):([0-9]{2})$/, "$1/$2/$3 $4:$5:$6");
return new Date(adjusted);
}
document.write(SetDate('2014-04-07T01:00:00Z') );
// outputs Mon Apr 07 2014 04:00:00 GMT+0300 (Middle East Daylight Time)
I am looking to format the date to 2014/04/07 04:00:00 but don't know what is wrong.
Thanks
You have forgotten to take account of the last character Z of your string in your pattern:
/^([0-9]{4})-([0-9]{2})-([0-9]{2}).([0-9]{2}):([0-9]{2}):([0-9]{2}).$/
^