I'm trying to parse the date which is in the following format
dateFormat: "d-M-y" // returns 10-Oct-13
I'm using jQuery UI for formatting date.
Below is my code:
var d1 = new Date("10-Oct-13");
alert(d1); //Invalid in FF and IE, works in chrome
Seems weird, here is my JSFiddle for reproducing the bug in FF and IE.
Note: I don't want to use plugin, since it is working chrome.
Please share your thoughts.
You can use Datepicker's parseDate() method in conjunction with the format string to parse the date:
var d1 = $.datepicker.parseDate("d-M-y", $("#lastAssimilationDate").val())
alert(d1); // alerts: Thu Oct 10 2013 00:00:00 GMT+0200
See the edited JSFiddle.
From the MDN doc for Date:
dateString
String value representing a date. The string should be in a format recognized by the parse method (IETF-compliant RFC 2822 timestamps).
Essentially you're passing a string in an unsupported date format as the dateString parameter of the constructor, so the JavaScript engine is (correctly) stating that it's an invalid date. Chrome seems to be slightly more forgiving with the date formats it allows, but that's non-standard.
You can use the getDate function to obtain a Date object representing your selected date:
var d1 = $('#lastAssimilationDate').datepicker("getDate");
Use the built-in getDate method:
$('button').click(function(){
var d1 = $("#lastAssimilationDate" ).datepicker('getDate');
console.log(d1);
});
You can also assign an altField with an altFormat of yyyy-mm-dd if you need to send an ISO-standard date to the server.
Related
I have a date string 'Mar 15/18' which I need to parse in order to get dd, mm and yyyy components separately.
When I parse this date string using the below code in Internet Explorer, -18 is returned:
var d='Mar 15/18';
var dt=new Date(d);
alert(dt.getFullYear());
This works fine in chrome and returns 2018. I am unable to think of any reason as to why it would return a negative value in IE.
Because Javascript getFullYear() is expecting a valid dateObj as its parameter and will return a four-digit year value.
'Mar 15/18' is not a standard dateObj of Javascript. Instead, you should update your value to a valid dateObj if you want to get the correct conversion in IE (Chrome is much smarter than IE).
More details can be found here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
Another handy way to deal with date and time object is to use moment.js library.
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 need to send a date to a backend service that requires a date in the following format.
I have access to moment also.
I am using an input type of datetime on the front end which sends over a date like this: "2017-05-17T10:00"
I have tried new Date("2017-05-17T10:00"); but this returns Wed May 17 2017 11:00:00 GMT+0100 (BST). I have also tried using some moment methods, but cannot get the correct format.
Does anyone know how I can convert the datetime string - "2017-05-17T11:43" to the following '2017-05-17T10:43:03+0100'?
Try moment.format(). Here is the list for reference https://momentjs.com/docs/#/displaying/format/
var dt = new Date("2017-05-17T10:00");
console.log(dt);
//'2017-05-17T10:43:03+0100'
var z = moment(dt).format("YYYY-MM-DDTHH:mm:ssZZ");
console.log(z);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
Your date is in ISO 8601 format. If you have access to Moment.js (as you said) you can use format() method as below:
var date = moment("2017-05-17T10:00").format("YYYY-MM-DDTHH:mmZZ");
console.log(date);
// prints "2017-05-17T10:00-0300"
Try it.
I am trying to parse a string to javascript Date object, I tried different ways to parse it to Date but none of them seems to work. Initially I was thinking it will be easy to parse string to Date as JavaScript Date has constructor that takes a string or I would use Date.parse() method but it seems that I was wrong.
Here is string for date format-
2015-12-01 00:28:28.1271204 +01:00
What I have tried so far-
var dateCalc = new Date(str);
var dateCalc = Date.parse(str);
Please this JSFiddle - http://jsfiddle.net/D7c28/12/
Please suggest solution for this. Please let me know if I am missing something.
Thanks :)
It works fine for me:
var str = "Fri, 15 Nov 2013 09:00:00 -0600"
var date = new Date(str);
console.log(date.getDate()) // 15
date is a Date object with many methods like getDate(). Check out the documentation.
Update:
2015-12-01 00:28:28.1271204 +01:00 seems not to be a valid date for the default constructor (but works fine in node on my Mac). So I use moment.js and it works fine.
Check out the updated jsfiddle.
I'm almost sure that
var dateString = "2015-12-01 00:28:28.1271204 +01:00";
var dateCalc = new Date(dateString);
Will work (dateCalc) will have a proper date (that is, Tue Dec 01 2015 00;28:28 GMT+0100.
If you want to be a more flxeible with the solution you always can try MomentJS which gives you a lot of possibilties with format, localization and such stuff.
Youtube's API returns a JSON object with an array of videos. Each video object has a published date formatted like "2012-01-11T20:49:59.415Z". If I initialize a Javascript Date object using the code below, the object returns "Invalid Date".
var dt = new Date( "2012-01-11T20:49:59.415Z" );
I'm using this on iOS/mobile Safari, if that makes a difference.
Any suggestions or ideas on how to create a valid object?
Try using JavaScript's Date.parse(string) and the Date constructor which takes the number of milliseconds since the epoch. The "parse" function should accept a valid ISO8601 date on any browser.
For example:
var d = new Date(Date.parse("2012-01-11T20:49:59.415Z"));
d.toString(); // => Wed Jan 11 2012 15:49:59 GMT-0500 (EST)
d.getTime(); // => 1326314999415
var dt = "2012-01-11T20:49:59.415Z".replace("T"," ").replace(/\..+/g,"")
dt = new Date( dt );
I ended up finding a solution at http://zetafleet.com/blog/javascript-dateparse-for-iso-8601. It looks like the date is in a format called 'ISO 8601.' On earlier browsers (Safari 4, Chrome 4, IE 6-8), ISO 8601 is not supported, so Date.parse doesn't work. The code referenced from the linked blog post extends the current Date class to support ISO 8601.
If you only need a portion of the date (eg. if you don't care about the time or time zone) you can just strip that portion of the date string off.
This page has code that parses youtube (ISO 8601) dates into a date object:
http://webcloud.se/log/JavaScript-and-ISO-8601/
Archive.org backup of same
It work for me, though I haven't tested it very much.