I have a date string which looks like this:
Thu Feb 09 2012 01:50:00 GMT+0000 (GMT Standard Time)
And I basically need to re-format it to be 2 strings (one for date and one for time).
The date format needs to be:
m-d-Y (01-16-2012)
and the time should be
H:i (10:30)
I have a date string which looks like this:
Thu Feb 09 2012 01:50:00 GMT+0000 (GMT Standard Time)
And I basically need to re-format it to be 2 strings (one for date and one for time).
The date format needs to be:
m-d-Y (01-16-2012)
and the time should be
Share Improve this question edited Nov 1, 2021 at 8:14 Nimantha 6,4716 gold badges31 silver badges76 bronze badges asked Jan 16, 2012 at 16:21 DarkMantisDarkMantis 1,5165 gold badges19 silver badges40 bronze badges 2H:i (10:30)
- My "H:i" and "m-d-Y" are from PHP, I didn't know the correct ones in JS. Sorry. – DarkMantis Commented Jan 16, 2012 at 16:25
- 1 This might be useful to you stackoverflow./questions/206790/… – DotNetUser Commented Jan 16, 2012 at 16:28
6 Answers
Reset to default 5I would remend you take a look at Datejs. It is an excellent JavaScript library for parsing dates. (Making use of jQuery for cleanliness)
<script type="text/javascript" src="http://www.datejs./build/date.js"></script>
<script type="text/javascript" src="https://ajax.googleapis./ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<div id="date"></div>
<div id="time"></div>
<script type="text/javascript">
var d = "Thu Feb 09 2012 01:50:00 GMT+0000";
var dout = Date.parse(d);
$("#date").text(dout.toString('M-d-yyyy'));
$("#time").text(dout.toString('HH:mm'));
</script>
You might want to take a look at Moment.js
I believe DateJS extends the native JavaScript Date Object and might not be what you want. I am pretty sure MomentJS ( is was inspired by DateJS ) does not extend the native Date Object.
Check out the docs for Moment.js, this should do the trick for you: http://momentjs./docs/#/parsing/string+format
var foo = moment( stringDateValue , 'MM-DD-YYYY hh:mm');
I'm not sure about the built-in Date
object, but Datejs
can do this.
Using Datejs' toString
method:
var now = new Date();
console.log(now.toString('MM-dd-yyyy')); // 01-16-2012
console.log(now.toString('HH:mm')); // 10:30
DEMO: http://jsfiddle/b3wgR/3
EDIT: If your date is from a string, Datejs can parse it for you.
var str = "Thu Feb 09 2012 01:50:00 GMT+0000";
var date = Date.parse(str);
console.log(date.toString('MM-dd-yyyy')); // 02-09-2012
console.log(date.toString('HH:mm')); // 01:50
DEMO: http://jsfiddle/RY5jb/
var td = new Date();
var weekday = new Array(7);
weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";
var month_name = new Array(12);
month_name[0] = "January";
month_name[1] = "February";
month_name[2] = "March";
month_name[3] = "April";
month_name[4] = "May";
month_name[5] = "June";
month_name[6] = "July";
month_name[7] = "August";
month_name[8] = "September";
month_name[9] = "October";
month_name[10] = "November";
month_name[11] = "December";
document.getElementById("TodayDate").innerHTML = weekday[td.getDay()] + ", " + month_name[td.getMonth()] + " " + td.getDate() + ", " + td.getFullYear();
Now output is like : Thursday, May 22, 2014
Demo: http://jsfiddle/satishverma143/xSAbK/
here is a polyfill for new Date().format("MM-DD-YYYY HH:mm");
it will return a string with whatever format you want
https://github./UziTech/js-date-format
//example: dateFormater("D, M n, Y h:ft", new Date(), 2)
//results: a formated date two days from now -> Sunday, February 10, 2013 3:07pm
//today is Friday 2/8/13
function dateFormater (mod, nDate, addDay) {
var daysFull = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var daysAbbr = ["Sun.", "Mon.", "Tue.", "Wed.", "Thu.", "Fri.", "Sat."];
var monthsFull = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthsAbbr = ["Jan.", "Feb.", "Mar.", "Apr.", "May", "Jun.", "Jul.", "Aug.", "Sep.", "Oct.", "Nov.", "Dec."];
var moddt = "";
var tmp;
//add days to date (use -1 to subtract days
if (typeof addDay !== 'undefined') nDate.setDate(nDate.getDate() + addDay);
var dateMod = mod.split("");
for (el in dateMod) {
switch(dateMod[el]) {
//Delimiters: ,=, /=/ " "=" " :=:
case ",":
moddt += dateMod[el];
break;
case "/":
moddt += dateMod[el];
break;
case " ":
moddt += dateMod[el];
break;
case ":":
moddt += dateMod[el];
break;
//Month: M=January m=Jan. J=01 j=1
case "M":
moddt += monthsFull[nDate.getMonth()];
break;
case "m":
moddt += monthsAbbr[nDate.getMonth()];
break;
case "J":
tmp = nDate.getDate();
tmp = tmp.toString();
if (tmp.length == 1) {
moddt += "0" + tmp;
} else {
moddt += tmp;
}
break;
case "j":
tmp = nDate.getDate();
moddt += tmp.toString();
break;
//Day: D=Monday d=Mon. N=01 n=1
case "D":
moddt += daysFull[nDate.getDay()];
break;
case "d":
moddt += daysAbbr[nDate.getDay()];
break;
case "N":
tmp = nDate.getDate();
tmp = tmp.toString();
if (tmp.length == 1) {
moddt += "0" + tmp;
} else {
moddt += tmp;
}
break;
case "n":
tmp = nDate.getDate();
moddt += tmp.toString();
break;
//Year: Y=2013 y=13
case "Y":
moddt += nDate.getFullYear();
break;
case "y":
tmp = nDate.getFullYear();
tmp = tmp.toString();
moddt += tmp.substr(2, 2);
break;
//Hour: H=08 h=8 Military: Z=14 z=01
case "H":
tmp = nDate.getHours();
if (tmp > 12) tmp -= 12;
tmp = tmp.toString();
if (tmp.length == 1) {
moddt += "0" + tmp;
} else {
moddt += tmp;
}
break;
case "h":
tmp = nDate.getHours();
if (tmp > 12) tmp -= 12;
moddt += tmp.toString();
break;
case "Z":
tmp = nDate.getHours();
tmp = tmp.toString();
if (tmp.length == 1) {
moddt += "0" + tmp;
} else {
moddt += tmp;
}
break;
case "z":
tmp = nDate.getHours();
moddt += tmp.toString();
break;
//Minute: f=08
case "f":
tmp = nDate.getMinutes();
tmp = tmp.toString();
if (tmp.length == 1) {
moddt += "0" + tmp;
} else {
moddt += tmp;
}
break;
//Seconds: s=08
case "s":
tmp = nDate.getSeconds();
tmp = tmp.toString();
if (tmp.length == 1) {
moddt += "0" + tmp;
} else {
moddt += tmp;
}
break;
//AM/PM: T=AM/PM t=am/pm q=a/p
case "T":
if (nDate.getHours() > 11) {
moddt += "PM";
} else {
moddt += "AM";
}
break;
case "t":
if (nDate.getHours() > 11) {
moddt += "pm";
} else {
moddt += "am";
}
break;
case "q":
if (nDate.getHours() > 11) {
moddt += "p";
} else {
moddt += "a";
}
break;
}
}
return moddt;
}