網頁制作poluoluo文章簡介:JS教程:日期格式轉換函數.
JS教程:日期格式轉換函數。
<script>
function str2date(str){
var d=null;
var reg=/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})\.(\d+)$/
if(arr=str.match(reg))d=new Date(Number(arr[1]),Number(arr[2])-1,Number(arr[3]),Number(arr[4]),Number(arr[5]),Number(arr[6]),Number(arr[7]))
return d;
}
function date2str(d){
var ret=d.getFullYear()+"-"
ret+=("00"+(d.getMonth()+1)).slice(-2)+"-"
ret+=("00"+d.getDate()).slice(-2)+" "
ret+=("00"+d.getHours()).slice(-2)+":"
ret+=("00"+d.getMinutes()).slice(-2)+":"
ret+=("00"+d.getSeconds()).slice(-2)+"."
return ret+d.getMilliseconds()
}
if(d=str2date("2002-06-01 12:34:56.789"))alert(d.toLocaleString())
document.write(date2str(new Date()))
</script>