I have a date string "Sunday, February 28, 2010" that I would like to convert to a js date object formatted # MM/DD/YYYY but don't know how. Any suggestions?
If you're running with jQuery you can use the datepicker UI library's parseDate function to convert your string to a date:
var d = $.datepicker.parseDate("DD, MM dd, yy", "Sunday, February 28, 2010");
and then follow it up with the formatDate method to get it to the string format you want
var datestrInNewFormat = $.datepicker.formatDate( "mm/dd/yy", d);
If you're not running with jQuery of course its probably not the best plan given you'd need jQuery core as well as the datepicker UI module... best to go with the suggestion from Segfault above to use date.js.
HTH
I would grab date.js or else you will need to roll your own formatting function.
var stringDate = "Sunday, February 28, 2010";
var months = ["January", "February", "March"]; // You add the rest :-)
var m = /(\w+) (\d+), (\d+)/.exec(stringDate);
var date = new Date(+m[3], months.indexOf(m[1]), +m[2]);
The indexOf method on arrays is only supported on newer browsers (i.e. not IE). You'll need to do the searching yourself or use one of the many libraries that provide the same functionality.
Also the code is lacking any error checking which should be added. (String not matching the regular expression, non existent months, etc.)
If you only need it once, it's overkill to load a plugin.
For a date "dd/mm/yyyy", this works for me:
new Date(d.date.substring(6, 10),d.date.substring(3, 5)-1,d.date.substring(0, 2));
Just invert month and day for mm/dd/yyyy, the syntax is new Date(y,m,d)
I used the javascript date funtion toLocaleDateString to get
var Today = new Date();
var r = Today.toLocaleDateString();
The result of r will be
11/29/2016
More info at:
http://www.w3schools.com/jsref/jsref_tolocaledatestring.asp
Use moment js for any date operation.
https://momentjs.com/
console.log(moment("Sunday, February 28, 2010").format('MM/DD/YYYY'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
Related
I have given a very unusual date format in string. I need to convert it to a JS date object and add up a few days. The last step is clear, but I don't know how to convert the string into the JS date object. Take a look at the string date: October 02, 2016
You should use moment.js
Syntax:
moment(dateString, format, locale)
var dateStr = "Oktober 02, 2016"
var d = moment(dateStr, "MMM DD, YYYY", 'de');
console.log(d.format("DD-MM-YYYY"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment-with-locales.min.js"></script>
Use Date.parse() to parse the date and get the Date object.
var dateObj = new Date('October 02, 2016')
Now, you can perform all the Date operations on dateObj
Given that everything is static here, I thing the best case for you might be to keep a map of your month's name against there number i.e say Oktober : 8. This way you will easily get around of any locale issue in any library.
Once above map is done, you can use .substring to separate your string for blank space and commas to get date and year easily.
Once you have all this you can use new Date constructor with months date and year field.
Hope this all is easily understood, so I am skipping any code here.
Using new Date() can be converted from string to date.
And using setDate() you can add days in the date and can convert the date back to string,
Please check below snippet for more understanding.
var someDate = new Date('October 02, 2016');
console.log(someDate);
console.log(new Date(someDate.setDate(someDate.getDate() + 5)).toString());
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.
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.
Hey guys I am retrieving a value from an input box and am using that value to turn into a Date for JavaScript the format is Y-m-d h:i:s. It works perfect in Chrome but any other browser says invalid Date
var old = $(".checked-in-time").val();
old = new Date(old);
UPDATE:
Here is what I am doing:
var current = new Date();
var old = $(".checked-in-time").val();
old = Date.parse(old , 'Y-m-d H:i:s');
var newEnd = current - old
minutes = parseInt((newEnd/(1000*60))%60);
var subtractedWaitTime = minutes;
Pretty much getting the time difference based on minutes.
Date.parse accepts a limited number of formats:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
Your format is not one of the ones supported. You can parse it yourself and just pass the arguments directly. One of the forms Date accepts is this, which would be easy enough to pull out from your format:
new Date(year, month[, date[, hour[, minutes[, seconds[, milliseconds]]]]]);
But I would recommend removing the pain of having to worry about cross browser compatibility and parsing things yourself, and use moment instead, where you can parse the date like this
moment(dateStr,'YYYY-M-D H:m:s')
and then if you needed to have it as a Javascript Date object you could just run
moment().toDate();
in the more likely case you just need to display it formatted somewhere, or compare it to other dates, moment offers many functions for formatting, manipulating and comparing dates
http://momentjs.com/docs/#/displaying/as-javascript-date/
Try one of the following formats
MM-dd-yyyy
yyyy/MM/dd
MM/dd/yyyy
MMMM dd, yyyy
MMM dd, yyyy
Could be that some browsers don't support yyyy-mm-dd
You can parse date like this,
var old = $(".checked-in-time").val();
old = Date.parseDate(old , 'yyyy-mm-dd h:i:s');
if the above not working, you can also try Y-m-d H:i:s this format. For me Y/m/d H:i worked fine.
You could try use such format Y-m-dTH:i:s, e.g. 2011-01-01T12:00:00
Or you can use moment library (Javascript Date library)
I am converting json date from something like:
/Date(1224043200000)/
to
Mon Oct 22 16:37:04 UTC+0800 2012
using
var date = new Date(parseInt(dateData.substr(6), 10));
Is there any way to change the format to just show the month, date and year (Oct 22, 2012) instead of including the timezone and current day using similar if not the same code as the one I'm already using? Thanks so much.
Checkout datejs which has powerful formatting capabilities. Using the toString FormatSpecifiers, you can provide a custom pattern like this:
new Date().toString("MMM dd yyyy");
You can try this:
var formattedDate = date.toString().split(" ").slice(1, 4).join(" ");
You may want to use momentjs
With something like this :
moment(1224043200000).format("MMM Do, YYYY");