我是可爱的标题啊
获取当前时间和时间函数
var myDate = new Date();//测试时间2018/12/28 上午10:15:17
myDate.getYear(); //获取当前年份(2位) 118
myDate.getFullYear(); //获取完整的年份(4位,1970-????)2018
myDate.getMonth(); //获取当前月份(0-11,0代表1月) 11
myDate.getDate(); //获取当前日(1-31) 28
myDate.getDay(); //获取当前星期X(0-6,0代表星期天) 5
myDate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数) 1545963317614
myDate.getHours(); //获取当前小时数(0-23) 10
myDate.getMinutes(); //获取当前分钟数(0-59) 15
myDate.getSeconds(); //获取当前秒数(0-59) 17
myDate.getMilliseconds(); //获取当前毫秒数(0-999) 617
myDate.toLocaleDateString(); //获取当前日期 2018/12/28
var mytime=myDate.toLocaleTimeString(); //获取当前时间 10:15:17
myDate.toLocaleString(); //获取日期与时间2018/12/28 上午10:15:17
//获取当前时间,格式YYYY-MM-DD
function getNowFormatDate() {
var date = new Date();
var seperator1 = "-";
var year = date.getFullYear();
var month = date.getMonth() + 1;
var strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
var currentdate = year + seperator1 + month + seperator1 + strDate;
return currentdate;
}
//求两个时间的天数差 日期格式为 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);
}
日期时间脚本库方法列表
Date.prototype.isLeapYear 判断闰年
Date.prototype.Format 日期格式化
Date.prototype.DateAdd 日期计算
Date.prototype.DateDiff 比较日期差
Date.prototype.toString 日期转字符串
Date.prototype.toArray 日期分割为数组
Date.prototype.DatePart 取日期的部分信息
Date.prototype.MaxDayOfDate 取日期所在月的最大天数
Date.prototype.WeekNumOfYear 判断日期所在年的第几周
StringToDate 字符串转日期型
IsValidDate 验证日期有效性
CheckDateTime 完整日期时间检查
daysBetween 日期天数差
//日期输出字符串,重载了系统的toString方法
Date.prototype.toString = function(showWeek)
{
var myDate= this;
var str = myDate.toLocaleDateString();
if (showWeek)
{
var Week = ['日','一','二','三','四','五','六'];
str += ' 星期' + Week[myDate.getDay()];
}
return str;
}
//把日期分割成数组
Date.prototype.toArray = function()
{
var myDate = this;
var myArray = Array();
myArray[0] = myDate.getFullYear();
myArray[1] = myDate.getMonth();
myArray[2] = myDate.getDate();
myArray[3] = myDate.getHours();
myArray[4] = myDate.getMinutes();
myArray[5] = myDate.getSeconds();
return myArray;
}
//取得当前日期所在月的最大天数
Date.prototype.MaxDayOfDate = function()
{
var myDate = this;
var ary = myDate.toArray();
var date1 = (new Date(ary[0],ary[1]+1,1));
var date2 = date1.dateAdd(1,'m',1);
var result = dateDiff(date1.Format('yyyy-MM-dd'),date2.Format('yyyy-MM-dd'));
return result;
}
//取得当前日期所在周是一年中的第几周
Date.prototype.WeekNumOfYear = function()
{
var myDate = this;
var ary = myDate.toArray();
var year = ary[0];
var month = ary[1]+1;
var day = ary[2];
document.write('< script language=VBScript\> \n');
document.write('myDate = DateValue(''+month+'-'+day+'-'+year+'') \n');
document.write('result = DatePart('ww', myDate) \n');
document.write(' \n');
return result;
}
//字符串转成日期类型
//格式 MM/dd/YYYY MM-dd-YYYY YYYY/MM/dd YYYY-MM-dd
function StringToDate(DateStr)
{
var converted = Date.parse(DateStr);
var myDate = new Date(converted);
if (isNaN(myDate))
{
var arys= DateStr.split('-');
myDate = new Date(arys[0],--arys[1],arys[2]);
}
return myDate;
}
当前月的第一天和最后一天
//格式化日期
function setDate(date)
{
y=date.getFullYear();
m=date.getMonth()+1;
d=date.getDate();
m=m<10?"0"+m:m;
d=d<10?"0"+d:d;
return y+"-"+m+"-"+d;
}
//获取当前月的第一天
function getCurrentMonthFirst()
{
var date=new Date();
date.setDate(1);
return date;
}
//获取当前月的最后一天
function getCurrentMonthLast(){
var date=new Date();
var currentMonth=date.getMonth();
var nextMonth=++currentMonth;
var nextMonthFirstDay=new Date(date.getFullYear(),nextMonth,1);
var oneDay=1000*60*60*24;
return new Date(nextMonthFirstDay-oneDay);
}