Following date formatting is not working in momentJs
var date ="01-12-2015";//DD-MM-YYY
console.info(moment(date).format('YYYY-MM-DD'));
How can I solve this?
You need to tell momentJS how to parse the string you are giving:
var date ="01-12-2015";//DD-MM-YYYY
console.info(moment(date, 'DD-MM-YYYY').format('YYYY-MM-DD'));
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.
Try this..
var date ="01-12-2015";//DD-MM-YYY
date = date.replace(/-/g,"/")
console.info(moment(new Date(date)).format('YYYY-MM-DD'));
Related
Why date is mismatch in javascript .I am getting this millisecond “-2208988800000” .I converted this using moment like this
moment(new Date(-2208988800000).toUTCString()).format('DD-MMM-YYYY')
Which give output “01-Jan-1900"” (which is correct)
Now I try to get again same long value or millisecond
moment(new Date("01-Jan-1900")).format('x')
"-2209008070000"
Why is mismatch in value ? "-2209008070000" and "-2208988800000" is not same
new Date("01-Jan-1900") is not something that works in every browser. Firefox for example outputs Invalid Date. The Date constructor has lots of quirks, and it's exactly why you should use a library like Moment.js to parse date and time strings.
Refer to the MDN documentation on Date and new Date(dateString) for additional details.
I think you are losing hours while converting to DD-MMM-YYYY
console.log(moment(new Date(-2208988800000).toUTCString()).format('DD-MMM-YYYY HH:mm:ss'))
//output of above line is input to below.
console.log(moment.parseZone(new Date("31-Dec-1899 19:00:00")).format('x'))
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://momentjs.com/downloads/moment.js"></script>
I've got a Datestring like this one: 20171010T022902.000Z and I need to create Javascript Date from this string. new Date('20171010T022902.000Z') would return Invalid Date.
I saw that it's possible to use moment.js for this purpose but I am not sure how I would specify the according format for my given example. I found this example from another thread:
var momentDate = moment('1890-09-30T23:59:59+01:16:20', 'YYYY-MM-DDTHH:mm:ss+-HH:mm:ss');
var jsDate = momentDate.toDate();
Question:
How can I create a JavaScript date from a given Datestring in this format: 20171010T022902.000Z (using moment)?
Your input (20171010T022902.000Z) matches known ISO 8601 so you can simply use moment(String) parsing method. In the Supported ISO 8601 strings section of the docs you will find:
20130208T080910.123 # Short date and time up to ms
Then you can use toDate() method
To get a copy of the native Date object that Moment.js wraps
Your code could be like the following
var m = moment('20171010T022902.000Z');
console.log( m.format() );
console.log( m.toDate() );
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
Note that this code does not shows Deprecation Warning (cited in Bergi's comment) because you input is in ISO 8601 known format. See this guide to know more about this warning.
Moreover "By default, moment parses and displays in local time" as stated here so format() will show the local value for your UTC input (20171010T022902.000Z ends with Z). See moment.utc(), utc() and Local vs UTC vs Offset guide to learn more about moment UTC mode.
I think you can do this without moment.js,.
Basically extract the parts you need using regex's capture groups, and then re-arrange into a correct format for new Date to work with.
var dtstr = '20171010T022902.000Z';
var dt = new Date(
dtstr.replace(/^(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})(\.\d{3}Z)$/,
"$1-$2-$3T$4:$5:$6$7"));
console.log(dt);
console.log(dt.toString());
If you are using moment.js anyway, this should work ->
var dt = moment("20171010T022902.000Z", "YYYYMMDDTHHmmss.SSSSZ");
console.log(dt.toString());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.1/moment.min.js"></script>
I have created a simple app, which need a date comparison. I used Moment.js, and I have tried on answer on this question:
Compare two dates in JS
Moment js date time comparison
How to compare only date in moment.js
But all of them not working for me.
and now I use this code:
if(moment('09/12/2016').isAfter('09/11/2016')){
console.log("True")
} else {
console.log("False")
}
But in the console it's thrown a warning:
Deprecation warning: moment construction falls back to js Date. This
is discouraged and will be removed in upcoming major release. Please
refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.
Everybody please help me. here's my fiddle https://jsfiddle.net/gq6ykw8L/
Your date string is ambiguous between DD/MM/YYYY and MM/DD/YYYY. If you refer to the link given in the warning (http://momentjs.com/guides/#/warnings/js-date/), it says:
This deprecation warning is thrown when no known format is found for a
date passed into the string constructor. To work around this issue,
specify a format for the string being passed to moment().
You need to use moment(String, Format) to specify the format of your date string.
moment('09/12/2016', 'DD/MM/YYYY');
moment('09/12/2016', 'MM/DD/YYYY');
I was looking for this very specific conversion which I couldnt find anywhere
var d = new Date("2014-12-25T18:30:00+0100");
console.log(d.toString());
the console.log returns an "Invalid Date"
The DateString is returned by the Facebook GraphAPI.
What am I doing wrong? can anyone help?
Thanks in advance
EDIT:
Now that I fixed the API my output is kind of consfusing:
I tried splitting up the String
d.getDay()+'.'+d.getMonth()+'.'+d.getYear()+' '+d.getHours()+':'+d.getMinutes();
it outputs
4.11.114 18:30
why?!
Instead of doing those complicated date functions
d.getDate()+'.'+d.getMonth()+'.'+d.getYear()+' '+d.getHours()+':'+d.getMinutes();
Do yourself a favour and include http://momentjs.com/ in your project. You can then simply take the date from the facebook api and format it with
moment("2014-12-25T18:30:00+0100").format("/* date format */");
See here for formating
SIDENOTE
When formating dates in plain javascript, you will have to add 1 month to your month - january is 0, that's why you get 4.11... instead of 4.12...
Change getYear() to getFullYear()
d.getDay()+'.'+d.getMonth()+'.'+d.getFullYear()+' '+d.getHours()+':'+d.getMinutes();
The page works fine in Chrome, but I have this one minor error in Firefox and a different problem in IE. Assistance with either of these issues is greatly appreciated. Since I've been stumped in the Firefox error the longest, I'll start with that one:
Here's the code: http://truxmapper.appspot.com/sched.html
The date picker selects a date using the format "07-08-2010 23:28". Now, I need to pass this time as a parameter to my servlet, which is expecting the time represented as a long. This is not a problem in Chrome. The Date object accepts a string in the format given above, but when I try to use getTime() on a date instantiated with a string in Firefox, it returns NaN. So what I've done in the on the page I linked to is a little handling asking the user to re-enter the dates if its read as NaN. This obviously isn't even a band-aid solution since even if you re-enter the date its still going to read NaN. I need to know why the Date function wont instantiate using the string you see in the input text field in Firefox.
In IE, for some reason its telling me that sTime is undefined.
That date format is ambiguous. Try it as yyyy-mm-dd instead of mm-dd-yyyy or dd-mm-yyyy.
Try
new Date(Date(dateString)).getTime()
(feels like an ugly workaround...)
Edit: This will produce wrong result.
The date format used in Javascript should be of the form YYYY MM DD HH:mm:ss. You can convert the format into this form with
// dateString = "07-08-2010 23:28";
dateString = dateString.replace(/(\d+) (\d+) (\d+)/, '$3-$1-$2');
But as mentioned in the comment, there is no standard Date format used by Javascript before the ECMAScript 5 standard. It is better to parse the dateString directly:
m = dateString.match(/(\d+)-(\d+)-(\d+) (\d+):(\d+)/)
date = new Date(+m[3], m[1]-1, +m[2], +m[4], +m[5]); // Note: January = 0.