We store every date data in ISO format using new Date().toISOString().
I tried to convert this ISO formatted date into Date object in node.js but I get Invalid Date response.
date string is isoDate = 2014-07-09T14:00:00.000Z
and I did console.log on Date.parse(isoDate); and new Date(isoDate);
but each returns NaN and Invalid Date.
I checked if the date string contains any invisible wrong character but they are fine and can be converted on browser console.
does this mean I need to convert the string manually and create Date object with parsed string?
Thanks for reading.
Try using moment library. It has a lot of functionality to work with dates and can easily be used both on client and server side. Calling moment("2014-07-09T14:00:00.000Z").toDate() would convert your string to a Date JavaScript Object, using this library.
I am posting this answer just in case somebody experience this like I did.
What happened to me is I thought I was sending an ISOString from the browser
{
startDate: date.startDate
}
which in fact I was sending a moment instance as parameter
When I checked in the network inspector I found out that the data being sent is in ISO format - yes, but it is enclosed in double quote ""
{
startDate: "2016-12-31T16:00:00.000Z"
}
it should not be enclosed in double qoutes and should look like this
{
startDate: 2016-12-31T16:00:00.000Z
}
what worked for me is to parse the moment to iso string
{
startDate: date.startDate.toISOString()
}
Related
I have a scenario where I can have a date coming as a string. It can be formatted as either:
2021-05-29
29-05-2021
Im using dateFns to parse the strings, but when the string is in the format 2021-05-29, it cant parse the string, which is why, when it has the format of 2021-05-29, it needs to be converted into 29-05-2021...
The parse() function from dateFns what actually what I needed to parse it to the correct format, but it doenst seem to work when passing 2021-05-29
return parse(date, 'mm-dd-yyyy', new Date());
Any ideas what how to handle this?
You could try reversing the date,
date = date.split(“-“).reverse().join(“-“)
How could I convert the string "2015-02-02" to ISODate 2015-02-02T00:00:00.000Z? I was trying to find some example but did not.
You can use the regular Javascript date functionality for this
new Date(dateString).toISOString()
from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
However, date parsing is very inconsistent across browsers so if you need this to be robust I would look into parsing using for example with Moment.js as this will allow you to specify a format string by which the date should be parsed like such
date = moment("12-25-1995", "YYYY-MM-DD");
date.format(); //will return an ISO representation of the date
from: http://momentjs.com/docs/#/parsing/string/
To change "2015-02-02" to "2015-02-02T00:00:00.000Z" simply append "T00:00:00.000Z":
console.log('2015-02-02' + 'T00:00:00.000Z');
Parsing to a Date and calling toISOString will fail in browsers that don't correctly parse ISO dates and those that don't have toISOString.
new Date("2015-02-02").toISOString()
new Date("11/11/2019").toISOString()
or use it as a variable
mydate = "11/11/2019"
new Date(mydate).toISOString()
I have a JSON output from a non-configurable system where various date/time variables have values like \/Date(1422691756316)\/
How can I get that into a readable format (I need to display it via PHP and Javascripts).
You could deserialize the JSON output via json_decode() and then you could try to parse the string to an timestamp with strtotime (http://php.net/manual/de/function.strtotime.php), then you could create a date from thins timestamp using the date() method (http://php.net/manual/de/function.date.php) with a given formatting string an the timestamp.
But I'm not sure if your date string is ready to be parsed. In all cases you may to remove the \/Date( and )\/ parts of the string.
EDIT: Looks like your string is not a timestamp, strtotime fails. You may have to check what kind of timestamp/ datestamp/ whatever your string is.
I am calling a .net asmx webservice that returns a number of fields. One of the fields in a date. The date is in the format of: "effective_date":"\/Date(978411600000)\/"
According to this SO question: How do I format a Microsoft JSON date? it would be better if the date returned was in ISO 8601 format, this way JavaScript would be able to interpret it as a date.
Currently I use the following javascript: new Date(d.effective_date) and I get the message Invalid Date. According to the linked SO question I should be able to do this if I can get the web service to pass the date in ISO format rather than in \/Date(978411600000)\/ format.
My question is, how do I get the webservice to return the date in ISO 8601 format?
Note:
I'm aware that I can use this (per the answer from the linked question): var date = new Date(parseInt(d.effective_date.substr(6)));, however it is mentioned in a comment that Incoming date values should be formatted in ISO-8601, so I'm wondering how to get the incoming date from the web service to be in this ISO format.
You may use:
var date = new Date(d.effective_date);
date.toISOString(); // ISO-8601 formatted string
JSFiddle: http://jsfiddle.net/nanndoj/gjtkvrsy/
I am trying to parse a date string i get from php through ajax call(which is irrelevant for now) using new Date().
however i keep getting wrong results.
My string is 2013-05-09 20:56:17
When i do
var something = new Date("2013-05-09 20:56:17");
alert(something.getMonth());
It keeps alerting 0
In my opinion for some reason new date cant parse this string.
Is there a way to specify the date format for new Date() in JS ?
My current solution is to import php's: date() and strtotime() and use them :
alert(date('m', strtotime("2013-05-09 20:56:17")));
This works however I have to use external js lib and I am pretty sure there is a better JS way to achieve that.
If you use slashes instead of hyphens, it works:
var something = new Date("2013/05/09 20:56:17");
alert(something.getMonth());
It's easy enough to replace any hyphens in a string with slashes first if you need to (say, if you were getting the date string from somewhere else):
var something = new Date("2013-05-09 20:56:17");
something = something.replace('-', '/');
It seems JavaScript's Date constructor doesn't recognize date formats with hyphens, or at least not that particular format.
Choose a different format specifier in PHP for your ajax dates. The format you expect and the format expected by the javascript are different.
var something = new Date("2013-05-09T20:56:17");
Note the 'T' which appears as a literal separator and marks the beginning of time per ISO 8601
Reference for various [browser] javascript date formats
W3 DateTime
Microsoft IE DateTime
Mozilla [Firefox] DateTime
Google DateJs
And lastly, the PHP date format specifier list:
PHP Date
PHP DateTime
Note the 'DATE_ISO8601'; but I suggest not using that at this time. Instead use 'DATE_ATOM' which may produce a date format more widely supported (comments suggest it makes iPhones happier and no issues with other browsers).
To use it in PHP:
$something = new DateTime('NOW');
echo $something->format('c');
echo $something->format(DateTime::ATOM);