用JavaScript格式化日期字符串
Formatting a Date string in JavaScript
我有一个日期字符串,如下所示:
2012年2月9日星期四格林尼治标准时间01:50:00(GMT标准时间)
我基本上需要将它重新格式化为2个字符串(一个表示日期,一个表示时间)。
日期格式需要为:
m-d-Y(01-16-2012)
时间应该是
H: i(10:30)
我建议您看看Datejs。它是一个用于解析日期的优秀JavaScript库。(利用jQuery进行清洁)
<script type="text/javascript" src="http://www.datejs.com/build/date.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/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>
您可能想看看Moment.js
我相信DateJS扩展了本地JavaScript日期对象,可能不是您想要的。我确信MomentJS(它的灵感来自DateJS)不会扩展原生的Date对象。
查看Moment.js的文档,这应该能帮你:http://momentjs.com/docs/#/parsing/string+格式化
var foo=时刻(字符串日期值,'MM-DD-YYYYhh:MM');
我不确定内置的Date
对象,但Datejs
可以做到这一点。
使用Datejs的toString
方法:
var now = new Date();
console.log(now.toString('MM-dd-yyyy')); // 01-16-2012
console.log(now.toString('HH:mm')); // 10:30
演示:http://jsfiddle.net/b3wgR/3
编辑:如果你的日期来自字符串,Datejs可以为你解析它。
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
演示:http://jsfiddle.net/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();
现在输出如下:2014年5月22日,星期四
演示:http://jsfiddle.net/satishverma143/xSAbK/
这里是新Date()的polyfill。格式("MM-DD-YYYY HH:MM");
它将返回一个字符串,无论你想要的格式是什么
https://github.com/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;
}
- 如何在 java 脚本中将字符串日期转换为 GMT
- 如何用字符串日期填充日期选择器表单
- 比较两个 JavaScript 字符串日期
- 解析字符串日期
- 分析具有不同时区的字符串日期
- 在 Javascript 中将字符串日期 YYYY-MM 转换为日期
- pickadate.js-3.5.3 将字符串日期转换为 mysql DATE 的正确格式
- 使用javascript jquery或css格式化字符串日期
- Javascript |字符串日期格式设置为2014年6月4日至2014年06月4日
- 字符串日期到unix时间戳
- *javascript*将字符串日期转换为UTC日期
- 如何拆分字符串(日期),以不同的字符分隔
- 如何格式化JavaScript长字符串日期
- 如何使用 Ajax 控件工具包 (CalendarExtender) 验证字符串日期
- 在javascript中转换字符串日期
- 如何在ASP中转换DateTime形式的字符串日期.净c#
- 将JSON字符串日期转换为JavaScript (Google Apps Script)
- 将javascript的字符串日期时间转换为c#的日期时间
- 在javascript中转换字符串日期到日期时间,如何处理日期格式
- 如何将JSON字符串日期转换为自定义格式日期