Following up from my previous post: Javascript Safari: new Date() with strings returns invalid date when typed
I am using Moment.js to convert a date string into a date field based on user input in the text box.
This is to prevent the problem I described in the linked post for Safari and Firefox not able to render the date when Chrome is fine.
Here is the code snipper:
var tempDate = moment(userInputFieldDate).format('DD-MM-YYYY');
alert(tempDate);
In Chrome, it does work fine (it use to work with the Javascript Date object too) but gives me the moment.js deprecation warning
Deprecation warning: moment construction falls back to js Date. This is discouraged and will be removed in upcoming major release. Please refer to https://github.com/moment/moment/issues/1407 for more info.
Arguments: [object Object]
Error
On Firefox and Safari is just gives an UNDEFINED DATE
in the alert window. So not entirely sure what should I be doing to convert the date string to a Date object.
Any suggestions on this issue?
Best Answer
If you are getting a JS based date
String
then first use thenew Date(String)
constructor and then pass theDate
object to themoment
method. Like:In case
dateString
is15-07-2016
, then you should use themoment(date:String, format:String)
method