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(“-“)
Related
I am using Date.parse() and I get NaN for this date string 01-Nov-2016
Is there a way I can pass a patern to the parse function? if not, what's the best way to parse this date format?
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.
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()
}
I'm passing UTC timestamp to moment js and timezone in order to get back the real date.
This is how I'm doing it:
formatDate: function(dateTime, timezone) {
var format = 'D-MMM-YYYY';
return moment.utc(dateTime, format).tz(timezone).format(format);
}
So I would pass on something like formatDate(1399922165, 'America/Los_Angeles'); and it returns 12-Jan-9992 instead of 12-May-2014.
If instead I do it like this:
moment(dateTime).tz(timezone).format(format);
Then it returns 16-Jan-1970.
Thanks to Ian, this ended up being the solution.
moment.unix(dateTime).tz(timezone).format(format);
Any ideas?
Thanks to Ian, this ended up being the solution.
moment.unix(dateTime).tz(timezone).format(format);
I was trying moment.utc() instead of moment.unix().
The strange results came from moment.utc(datetime, format) expecting datetime to match format. However, it's important to note that moment.utc(datetime) still returns 1970 year result so it still wouldn't have returned the desired result even without the format.
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);