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?
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(“-“)
2018-06-04T01:00:45.500Z
Moment.js returns a date object when calling moment(), but sometimes returns this timestamp.
How can I make sure I'm receiving a timestamp like this and not a date object?
For example, output of calling console.log(moment()):
Did you try this?
moment().format('YYYY-MM-DDTHH:mm:ssZ');
This returns an output in this format :
2018-06-04T12:49:53+10:00
That format is ISO, is the universal format to represent a date in javascript. It's the same format that you get from Date.toISOString()
You can get this date format as follow
console.log(moment().toISOString())
reference https://momentjs.com/docs/#/displaying/as-iso-string/
My string is like 3/29/2016 17:30.How to convert it into datetime in javascript?
and I want to compare it with another date?
var date= new Date("2016-03-29 17:30:00".replace(/-/g,"/"));
if date is dynamic then you have to break that using substring method and concat in above 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()
}