Js对Date对象操作详解

 
  
var date=new Date();//获取当前时间。
console.log(date);
var str=date.getTime();
console.log(str);

/* 获取当天的时间(常用格式)*/
var year=date.getFullYear();//integer类型
var month=date.getMonth()+1;
var day=date.getDate();
var hour=date.getHours();
var min=date.getMinutes();
var seconds=date.getSeconds();
var today = year + "-";
if(month < 10)
    today += "0";
today += month + "-";
if(day < 10)
    today += "0";
today += day + " ";
if(hour < 10)
    today += "0";
today += hour + ":";
if (min < 10) today += '0';
today += min + ":";
if (seconds < 10) today += '0';
today += seconds;
window.console.log(today);

/*获取星期天数*/
var Xday=date.getDay();
switch(Xday){
    case 0:Xday="日";break;
    case 1:Xday="一";break;
    case 2:Xday="二";break;
    case 3:Xday="三";break;
    case 4:Xday="四";break;
    case 5:Xday="五";break;
    case 6:Xday="六";break;
}
window.console.log("星期"+Xday);

/*将字符串转换为日期*/
str=today.replace(/-/g,"/");//转换为标准时间格式
console.log(str);
str = new Date(str);
console.log(str);
str=str.getTime();//getTime() .方法可返回距 1970 年 1 月 1 日之间的毫秒数
console.log(str);


你可能感兴趣的:(JS)