var s = "2009年2月28日 12:23" var re = /[年月-]/g; s = s.replace(re,"/") var re = /[日]/g; s = s.replace(re,"") var dt = new Date(s); var y = dt.getFullYear(); var m = dt.getMonth()+1; var d = dt.getDate(); var h = dt.getHours(); var n = dt.getMinutes(); m = m<10?'0'+m:m; d = d<10?'0'+d:d; h = h<10?'0'+h:h; n = n<10?'0'+n:n; s = y + "-" + m + "-" + d + " " + h + ":" + n alert(s)
toLocaleString() 根据本地时间格式,把 Date 对象转换为字符串。 1 2 3 toLocaleTimeString() 根据本地时间格式,把 Date 对象的时间部分转换为字符串 1 2 3 toLocaleDateString() 根据本地时间格式,把 Date 对象的日期部分转换为字符串
最后面有一个例子<script> /* * 日期转字符串 * format 格式。年、月、日、时、分、秒、毫秒分别用 yyyy、MM、dd、HH、mm、ss、S 表示(注意大小写) */ Date.prototype.format = function(format){ var o = { "M+" : this.getMonth()+1, //month "d+" : this.getDate(), //day "h+" : this.getHours(), //hour "m+" : this.getMinutes(), //minute "s+" : this.getSeconds(), //second "q+" : Math.floor((this.getMonth()+3)/3), //quarter "S" : this.getMilliseconds() //millisecond } if(/(y+)/.test(format)){ format=format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); } for(var k in o){ if(new RegExp("("+ k +")").test(format)){ format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length)); } } return format; }/* * 字符串转日期 * format 格式。年、月、日、时、分、秒、毫秒分别用 yyyy、MM、dd、HH、mm、ss、SSS 表示(注意大小写) */ String.prototype.toDate = function(style) { if (style == null) style = 'yyyy-MM-dd hh:mm:ss'; var o = { 'y+' : 'y', 'M+' : 'M', 'd+' : 'd', 'h+' : 'h', 'm+' : 'm', 's+' : 's', 'S+' : 'S' }; var result = { 'y' : '', 'M' : '', 'd' : '', 'h' : '00', 'm' : '00', 's' : '00', 'S' : '000' } var tmp = style; for (var k in o) { if (new RegExp('(' + k + ')').test(style)) { result[o[k]] = this.substring(tmp.indexOf(RegExp.$1), tmp.indexOf(RegExp.$1) + RegExp.$1.length); } } return new Date(result['y'], result['M'] - 1, result['d'], result['h'], result['m'], result['s'], result['S']); };// 例子 alert("01 13 51 2008年-09月-10 999".toDate("mm hh ss yyyy年-MM月-dd SSS").format("yyyy年MM月dd日 hh时mm分ss秒S毫秒")); </script>
window.onload=function(){ var s="2009年8月23日 12:23" var day=s.split(" ")[1]; var t=s.split("日 ")[0].replace(/[^\x00-\xff]/g, '-'); var date=t+" "+day; alert(date) } </script> </head> <body>
var s = "2009年2月28日 12:23"
var re = /[年月-]/g;
s = s.replace(re,"/")
var re = /[日]/g;
s = s.replace(re,"")
var dt = new Date(s);
var y = dt.getFullYear();
var m = dt.getMonth()+1;
var d = dt.getDate();
var h = dt.getHours();
var n = dt.getMinutes();
m = m<10?'0'+m:m;
d = d<10?'0'+d:d;
h = h<10?'0'+h:h;
n = n<10?'0'+n:n;
s = y + "-" + m + "-" + d + " " + h + ":" + n
alert(s)
toLocaleTimeString() 根据本地时间格式,把 Date 对象的时间部分转换为字符串 1 2 3
toLocaleDateString() 根据本地时间格式,把 Date 对象的日期部分转换为字符串
/*
* 日期转字符串
* format 格式。年、月、日、时、分、秒、毫秒分别用 yyyy、MM、dd、HH、mm、ss、S 表示(注意大小写)
*/
Date.prototype.format = function(format){
var o = {
"M+" : this.getMonth()+1, //month
"d+" : this.getDate(), //day
"h+" : this.getHours(), //hour
"m+" : this.getMinutes(), //minute
"s+" : this.getSeconds(), //second
"q+" : Math.floor((this.getMonth()+3)/3), //quarter
"S" : this.getMilliseconds() //millisecond
}
if(/(y+)/.test(format)){
format=format.replace(RegExp.$1,
(this.getFullYear()+"").substr(4 - RegExp.$1.length));
}
for(var k in o){
if(new RegExp("("+ k +")").test(format)){
format = format.replace(RegExp.$1,
RegExp.$1.length==1 ? o[k] :
("00"+ o[k]).substr((""+ o[k]).length));
}
}
return format;
}/*
* 字符串转日期
* format 格式。年、月、日、时、分、秒、毫秒分别用 yyyy、MM、dd、HH、mm、ss、SSS 表示(注意大小写)
*/
String.prototype.toDate = function(style) {
if (style == null) style = 'yyyy-MM-dd hh:mm:ss';
var o = {
'y+' : 'y',
'M+' : 'M',
'd+' : 'd',
'h+' : 'h',
'm+' : 'm',
's+' : 's',
'S+' : 'S'
};
var result = {
'y' : '',
'M' : '',
'd' : '',
'h' : '00',
'm' : '00',
's' : '00',
'S' : '000'
}
var tmp = style;
for (var k in o) {
if (new RegExp('(' + k + ')').test(style)) {
result[o[k]] = this.substring(tmp.indexOf(RegExp.$1),
tmp.indexOf(RegExp.$1) + RegExp.$1.length);
}
}
return new Date(result['y'], result['M'] - 1, result['d'], result['h'],
result['m'], result['s'], result['S']);
};// 例子
alert("01 13 51 2008年-09月-10 999".toDate("mm hh ss yyyy年-MM月-dd SSS").format("yyyy年MM月dd日 hh时mm分ss秒S毫秒"));
</script>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title></title> <script>
window.onload=function(){
var s="2009年8月23日 12:23"
var day=s.split(" ")[1];
var t=s.split("日 ")[0].replace(/[^\x00-\xff]/g, '-');
var date=t+" "+day;
alert(date) }
</script>
</head>
<body>
</body>
</html>