Javascript datetime parsing from string - javascript

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.

Related

change date format javascript

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.

How can I set up a correct format while doing new Date() in jQuery?

I have a date in a following format:
12/11/2015 07:12 PM
In jQuery I'm doing:
var parsedDate2 = new Date(date);
alert(parsedDate2);
And that prints me:
Fri Dec 11 2015 07:12:00 GMT+0100 (Central European Standard Time)
and that almost works correctly, mostly because in my example (12/11/2015 07:12 PM) the format is DD/MM and not MM/DD. However, jQuery treats it as the month is first. That's a problem, because when I chose as input:
19/11/2015 07:17 PM <--- (19th of November)
I'm getting:
Invalid date
So how can I set up the correct format here with the day before the month?
Ugly, but it work, with JS only :
a = "12/11/2015 07:12 PM";
b = a.split(' ');
c = b[0].split('/');
bad = new Date(a);
alert('bad : '+bad);
good = new Date(c[1]+'/'+c[0]+'/'+c[2]+' '+b[1]+' '+b[2]);
alert('good : '+good);
The other way is to use Moment.js parsing tool
Think that you should use more specialized and focused library along with JQuery, for me the best one is Moment.js - it has all and more than needed to date-time parsing and formatting and doesn't do something else.
Also, there are some other alternatives, like date.js and globalize.js
It's in the form of mm/dd/yyyy. Try 11/19/2015 07:17 PM. Sadly, jQuery doesn't know which format you're using and so, uses the deafult one.
Unfortunately, the Javascript Date system isn't very malleable when it comes to adding date formats. Here is a reference from Mozilla. I think wierdpanda has the right idea, write a function that accepts your date format, reformats it before feeding it to new Date(), and returns the result. Use this in place of where you have new Date(), and all should be good.

Unable to parse the date

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.

Use JavaScript to convert a date string with timezone to a date object in local time

The format of my date string looks like this: yyyy-MM-ddTHH:mm:ss-0Z00
Example 1: 2010-03-05T07:03:51-0800
Example 2: 2010-07-01T20:23:00-0700
I need to create a date object using these date strings. new Date() does not work on this string.
Please help me convert these date strings into a date objects with the local timezone.
Thank you!
Edit: I am using this in Pentaho Data Integration 4.3.0.
Take my timezone as an example (AEST):
function parseDate(str_date) {
return new Date(Date.parse(str_date));
}
var str_date = "2015-05-01T22:00:00+10:00"; //AEST time
var locale_date = parseDate(str_date);
locale_date: Fri May 01 2015 22:00:00 GMT+1000 (AEST)
var str_date = "2015-05-01T22:00:00+00:00" //UTC time
var locale_date = parseDate(str_date);
locale_date: Sat May 02 2015 08:00:00 GMT+1000 (AEST)
You can use a library such as Moment.js to do this.
See the String + Format parsing.
http://momentjs.com/docs/#/parsing/string-format/
The following should parse your date you provided, but you may need to modify it for your needs.
var oldDate = "2010-03-05T07:03:51-0800";
var dateObj = moment(oldDate, "YYY-MM-DDTHH:mm:ssZ").toDate();
Alternatively, see Moment's String parser, which looks like it is in the format you provided, with the exception of a space between the seconds of the time and the time zone.
http://momentjs.com/docs/#/parsing/string/
Alternative
A second way of doing this is Date.js, another library that seems to parse the format just fine. http://www.datejs.com
Date String:
var strDate = "2010-07-01T20:23:00-0700";
To local time representation in native JS Date object:
var ltzDate = (new Date(strDate)).toLocaleString();

Parsing a Youtube API Date in Javascript

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.

Categories