[学习笔记]JS获取当前日期,年月日

来源:互联网 发布:php 访问sqlserver 编辑:程序博客网 时间:2024/06/09 13:52
第一种方法:
function CurentTimeMonth()

var now = new Date();

var year = now.getFullYear(); //年
var month = now.getMonth() + 1; //月
var day = now.getDate(); //日

var hh = now.getHours(); //时
var mm = now.getMinutes(); //分

var clock = year + "-";

if(month < 10)
clock += "0";

clock += month;

return (clock); 
}

第二种方法:
function formatDate() {
var d=new Date(); // 获取当前日期
var m=d.getMonth()+1;

if(m<10){
m="0"+m;
}
var day=d.getDate();
if(day<10){
day="0"+day;
}
var str=d.getYear()+"-"+(m)+"-"+day+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds(); 
return str;
}
0 0
原创粉丝点击