Kir*_*ran 51 javascript string date
这是我需要做的.
获取日期,转换为字符串并将其传递给第三方实用程序.当我通过时,库中的响应将以字符串格式显示日期.所以,我需要将日期转换为字符串,如20110506105524(YYYYMMDDHHMMSS)
function printDate() {
var temp = new Date();
var dateStr = temp.getFullYear().toString() +
temp.getMonth().toString() +
temp.getDate().toString() +
temp.getHours().toString() +
temp.getMinutes().toString() +
temp.getSeconds().toString();
debug (dateStr );
}
Run Code Online (Sandbox Code Playgroud)
上面的问题是,对于1-9个月,它会打印一个数字.如何更改它以正确打印月份,日期的2位数...
Ale*_* K. 58
如果单个数字和注释getMonth
返回0..11而不是1..12,则需要使用"0" 填充
function printDate() {
var temp = new Date();
var dateStr = padStr(temp.getFullYear()) +
padStr(1 + temp.getMonth()) +
padStr(temp.getDate()) +
padStr(temp.getHours()) +
padStr(temp.getMinutes()) +
padStr(temp.getSeconds());
debug (dateStr );
}
function padStr(i) {
return (i < 10) ? "0" + i : "" + i;
}
Run Code Online (Sandbox Code Playgroud)
Sté*_*ane 38
依赖于JQuery Datepicker,但它可以轻松完成:
var mydate = new Date();
$.datepicker.formatDate('yy-mm-dd', mydate);
Run Code Online (Sandbox Code Playgroud)
使用这个 polyfill https://github.com/UziTech/js-date-format
var d = new Date("1/1/2014 10:00 am");
d.format("DDDD 'the' DS 'of' MMMM YYYY h:mm TT");
//output: Wednesday the 1st of January 2014 10:00 AM
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
99753 次 |
最近记录: |