If you are getting a JS based date String
then first use the new Date(String)
constructor and then pass the Date
object to the moment
method. Like:
var dateString = 'Thu Jul 15 2016 19:31:44 GMT+0200 (CEST)';
var dateObj = new Date(dateString);
var momentObj = moment(dateObj);
var momentString = momentObj.format('YYYY-MM-DD'); // 2016-07-15
In case dateString
is 15-07-2016
, then you should use the moment(date:String, format:String)
method
var dateString = '07-15-2016';
var momentObj = moment(dateString, 'MM-DD-YYYY');
var momentString = momentObj.format('YYYY-MM-DD'); // 2016-07-15
Check out all their awesome documentation!
Here is where they discuss the Warning Message.
String + Format
Warning: Browser support for parsing strings is inconsistent. Because there is no specification on which formats should be supported, what works in some browsers will not work in other browsers.
For consistent results parsing anything other than ISO 8601 strings, you should use String + Format.
moment("12-25-1995", "MM-DD-YYYY");
String + Formats (multiple formats)
If you have more than one format, check out their String + Formats (with an 's').
If you don't know the exact format of an input string, but know it could be one of many, you can use an array of formats.
moment("12-25-1995", ["MM-DD-YYYY", "YYYY-MM-DD"]);
Please checkout the documentation for anything more specific.
Timezone
Checkout Parsing in Zone, the equivalent documentation for timezones.
The moment.tz constructor takes all the same arguments as the moment constructor, but uses the last argument as a time zone identifier.
var b = moment.tz("May 12th 2014 8PM", "MMM Do YYYY hA", "America/Toronto");
EDIT
//...
var dateFormat = "YYYY-M-D H:m"; //<-------- This part will get rid of the warning.
var aus1_s, aus2_s, aus3_s, aus4_s, aus5_s, aus6_s, aus6_e;
if ($("#custom1 :selected").val() == "AU" ) {
var region = 'Australia/Sydney';
aus1_s = moment.tz('2016-9-26 19:30', dateFormat, region);
aus2_s = moment.tz('2016-10-2 19:30', dateFormat, region);
aus3_s = moment.tz('2016-10-9 19:30', dateFormat, region);
aus4_s = moment.tz('2016-10-16 19:30', dateFormat, region);
aus5_s = moment.tz('2016-10-23 19:30', dateFormat, region);
aus6_s = moment.tz('2016-10-30 19:30', dateFormat, region);
aus6_e = moment.tz('2016-11-5 19:30', dateFormat, region);
} else if ($("#custom1 :selected").val() == "NZ" ) {
var region = 'Pacific/Auckland';
aus1_s = moment.tz('2016-9-28 20:30', dateFormat, region);
aus2_s = moment.tz('2016-10-4 20:30', dateFormat, region);
aus3_s = moment.tz('2016-10-11 20:30', dateFormat, region);
aus4_s = moment.tz('2016-10-18 20:30', dateFormat, region);
aus5_s = moment.tz('2016-10-25 20:30', dateFormat, region);
aus6_s = moment.tz('2016-11-2 20:30', dateFormat, region);
aus6_e = moment.tz('2016-11-9 20:30', dateFormat, region);
}
//...
Best Answer
To get rid of the warning, you need to either:
Pass in an ISO formatted version of your date string:
moment('2014-04-23T09:54:51');
Pass in the string you have now, but tell Moment what format the string is in:
moment('Wed, 23 Apr 2014 09:54:51 +0000', 'ddd, DD MMM YYYY HH:mm:ss ZZ');
Convert your string to a JavaScript Date object and then pass that into Moment:
moment(new Date('Wed, 23 Apr 2014 09:54:51 +0000'));
The last option is a built-in fallback that Moment supports for now, with the deprecated console warning. They say they won't support this fallback in future releases. They explain that using
new Date('my date')
is too unpredictable.