JS to get the current date and time
var myDate = new Date();
myDate.getYear(); //Get the current year (2 digits) myDate.getFullYear(); //Get the full year (4 digits, 1970-????) myDate.getMonth(); //Get the current month ( 0-11,0 represents January) myDate.getDate(); //Get the current day (1-31) myDate.getDay(); //Get the current week X (0-6,0 represent Sunday) myDate.getTime( ); //Get the current time (the number of milliseconds since 1970.1.1) myDate.getHours(); //Get the current hour (0-23) myDate.getMinutes(); //Get the current minute (0-59 ) myDate.getSeconds(); //Get the current number of seconds (0-59) myDate.getMilliseconds(); //Get the current number of milliseconds (0-999) myDate.toLocaleDateString(); //Get the current date var mytime=myDate .toLocaleTimeString(); //Get the current time myDate.toLocaleString( ); //Get the date and time
List of datetime script library methods
Date .prototype.isLeapYear //Judge leap year Date.prototype.Format
//Date formatting
Date.prototype.DateAdd //Date calculation
Date.prototype.DateDiff //Compare date difference
Date.prototype.toString //Date to string
Date .prototype.toArray //The date is divided into arrays Date.prototype.DatePart //
The partial information of the
date is taken Date.prototype.MaxDayOfDate //The maximum number of days
in the month of the date is taken Date.prototype.WeekNumOfYear //The number of the year in which the date is determined Week
StringToDate //String to date type
IsValidDate //Verify date validity
CheckDateTime //Complete date and time check
daysBetween //Date days difference
Determining leap years
date format
// format YYYY/yyyy/YY/yy for year // MM/M month // W/w week // dd/DD/d/D date // hh/HH/h/H time // mm /m minutes // ss/SS/s/S seconds
{
var str = formatStr;
var Week = ['日','一','二','三','四','五','六'];
str=str.replace(/yyyy|YYYY/,this.getFullYear());
str=str.replace(/yy|YY/,(this.getYear() % 100)>9?(this.getYear() % 100).toString():'0' + (this.getYear() % 100));
str=str.replace(/MM/,this.getMonth()>9?this.getMonth().toString():'0' + this.getMonth());
str=str.replace(/M/g,this.getMonth());
str=str.replace(/w|W/g,Week[this.getDay()]);
str=str.replace(/dd|DD/,this.getDate()>9?this.getDate().toString():'0' + this.getDate());
str=str.replace(/d|D/g,this.getDate());
str=str.replace(/hh|HH/,this.getHours()>9?this.getHours().toString():'0' + this.getHours());
str=str.replace(/h|H/g,this.getHours());
str=str.replace(/mm/,this.getMinutes()>9?this.getMinutes().toString():'0' + this.getMinutes());
str=str.replace(/m/g,this.getMinutes());
str=str.replace(/ss|SS/,this.getSeconds()>9?this.getSeconds().toString():'0' + this.getSeconds());
str=str.replace(/s|S/g,this.getSeconds());
return str;
}
求两个时间的天数差 日期格式为 YYYY-MM-dd
function daysBetween(DateOne,DateTwo)
{
var OneMonth = DateOne.substring(5,DateOne.lastIndexOf ('-'));
var OneDay = DateOne.substring(DateOne.length,DateOne.lastIndexOf ('-')+1);
var OneYear = DateOne.substring(0,DateOne.indexOf ('-'));
var TwoMonth = DateTwo.substring(5,DateTwo.lastIndexOf ('-'));
var TwoDay = DateTwo.substring(DateTwo.length,DateTwo.lastIndexOf ('-')+1);
var TwoYear = DateTwo.substring(0,DateTwo.indexOf ('-'));
var cha=((Date.parse(OneMonth+'/'+OneDay+'/'+OneYear)- Date.parse(TwoMonth+'/'+TwoDay+'/'+TwoYear))/86400000);
return Math.abs(cha);
}
若要显示:当前日期加时间(如:2018-06-12 12:00)
function CurentTime()
{
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 + "-";
if(day < 10)
clock += "0";
clock += day + " ";
if(hh < 10)
clock += "0";
clock += hh + ":";
if (mm < 10) clock += '0';
clock += mm;
return(clock);
}
-----------------------------------------------end-----------------------------------------------
文章来源:http://www.cnblogs.com/carekee/articles/1678041.html