js常用日期时间写法收集整理

写法1:

var today = new Date();

var strToday=today.getFullYear()+"-" + (today.getMonth()+1) + "-" + today.getDate()+" "+today.getHours()+":00:00";

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

写法2:

var now , nowYes , startTime , endTime , stationName;

function getNow(s) {  return s < 10 ? '0' + s : s; }

var myDate = new Date();

var year = myDate.getFullYear();

var month = myDate.getMonth() + 1;

var date = myDate.getDate();

var h = myDate.getHours();      //获取当前小时数(0-23)

var m = myDate.getMinutes();    //获取当前分钟数(0-59)

var s = myDate.getSeconds();

当前天

var now = year + '-' + getNow(month) + "-" + getNow(date) + " " + getNow(h)+":" + getNow(m)+":"+ getNow(s) ;

提前一天

var myDateYester = new Date(myDate.getTime() - 24*60*60*1000);

var yearYes = myDateYester.getFullYear();

var monthYes = myDateYester.getMonth() + 1;

var dateYes = myDateYester.getDate();

var hYes = myDateYester.getHours();      //获取当前小时数(0-23)

var mYes = myDateYester.getMinutes();    //获取当前分钟数(0-59)

var sYes = myDateYester.getSeconds();

var nowYes = yearYes + '-' + getNow(monthYes) + "-" + getNow(dateYes) + " " + getNow(hYes)+":" + getNow(mYes)+":"+ getNow(sYes) ;

$("#startTime").val(nowYes);

$("#endTime").val(now);

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

var myDate = new Date;

var year = myDate.getFullYear(); //获取当前年

var mon = myDate.getMonth() + 1; //获取当前月

var date = myDate.getDate(); //获取当前日

var h = myDate.getHours();//获取当前小时数(0-23)

var m = myDate.getMinutes();//获取当前分钟数(0-59)

var s = myDate.getSeconds();//获取当前秒

var week = myDate.getDay();

var weeks = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];

console.log(year, mon, date, weeks[week])

$("#time").html(year + "年" + mon + "月" + date + "日" + weeks[week]);

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

var mydate = new Date();

mydate.getYear(); //获取当前年份(2位)

mydate.getFullYear(); //获取完整的年份(4位,1970-????)

mydate.getMonth(); //获取当前月份(0-11,0代表1月)

mydate.getDate(); //获取当前日(1-31)

mydate.getDay(); //获取当前星期X(0-6,0代表星期天)

mydate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数)

mydate.getHours(); //获取当前小时数(0-23)

mydate.getMinutes(); //获取当前分钟数(0-59)

mydate.getSeconds(); //获取当前秒数(0-59)

mydate.getMilliseconds(); //获取当前毫秒数(0-999)

mydate.toLocaleDateString(); //获取当前日期

var mytime=mydate.toLocaleTimeString(); //获取当前时间

mydate.toLocaleString( ); //获取日期与时间

你可能感兴趣的:(js常用日期时间写法收集整理)