使⽤jquery如何获取时间
这篇⽂章主要介绍了jquery获取当前时间,⽉份,年份等,涉及针对⽇期时间的操作技巧,具有⼀定参考借鉴价值,需要的朋友可以参考下。
js(jQuery)获取时间的⽅法及常⽤时间类
$(function(){
var mydate = new Date();
var LocaleString();
/*alert(t);*/
$("#time").text(t);
$("#time").load("Untitled-1.html");
});
<P id="time">afwef</P>
获取JavaScript 的时间使⽤内置的Date函数完成
var mydate = new Date();
var LocaleTimeString(); //获取当前时间
⽇期时间脚本库⽅法列表
Date.prototype.isLeapYear 判断闰年
Date.prototype.Format ⽇期格式化
Date.prototype.DateAdd ⽇期计算
Date.prototype.DateDiff ⽐较⽇期差
String ⽇期转字符串
Array ⽇期分割为数组
Date.prototype.DatePart 取⽇期的部分信息
Date.prototype.MaxDayOfDate 取⽇期所在⽉的最⼤天数
Date.prototype.WeekNumOfYear 判断⽇期所在年的第⼏周
StringToDate 字符串转⽇期型
IsValidDate 验证⽇期有效性
CheckDateTime 完整⽇期时间检查
daysBetween ⽇期天数差
//---------------------------------------------------
// 判断闰年
//---------------------------------------------------
Date.prototype.isLeapYear = function()
{
return (0==Year()%4&&((Year()0!=0)||(Year()@0==0)));
}
//---------------------------------------------------
// ⽇期格式化
// 格式 YYYY/yyyy/YY/yy 表⽰年份
// MM/M ⽉份
// W/w 星期
// dd/DD/d/D ⽇期
// hh/HH/h/H 时间
// mm/m 分钟
/
/ ss/SS/s/S 秒
//---------------------------------------------------
Date.prototype.Format = function(formatStr)
{
var str = formatStr;
var Week = ['⽇','⼀','⼆','三','四','五','六'];
place(/yyyy|YYYY/,FullYear());
place(/yy|YY/,(Year() % 100)>9?(Year() % 100).toString():'0' + (Year() % 100));
place(/MM/,Month()>Month().toString():'0' + Month());
place(/M/Month());
place(/w|W/g,Day()]);
place(/dd|DD/,Date()>Date().toString():'0' + Date());
place(/d|D/Date());
place(/hh|HH/,Hours()>Hours().toString():'0' + Hours());
place(/h|H/Hours());
place(/mm/,Minutes()>Minutes().toString():'0' + Minutes());
place(/m/Minutes());
place(/ss|SS/,Seconds()>Seconds().toString():'0' + Seconds());
place(/s|S/Seconds());
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);
}
//+---------------------------------------------------
//| ⽇期计算
//+---------------------------------------------------
Date.prototype.DateAdd = function(strInterval, Number) {
var dtTmp = this;
switch (strInterval) {
case 's' :return new Date(Date.parse(dtTmp) + (1000 * Number));
case 'n' :return new Date(Date.parse(dtTmp) + (60000 * Number));
case 'h' :return new Date(Date.parse(dtTmp) + (3600000 * Number));
case 'd' :return new Date(Date.parse(dtTmp) + (86400000 * Number));
case 'w' :return new Date(Date.parse(dtTmp) + ((86400000 * 7) * Number));
case 'q' :return new FullYear(), (Month()) + Number*3, Date(), Hours(), Minutes(), Seconds()); case 'm' :return new FullYear(), (Month()) + Number, Date(), Hours(), Minutes(), Seconds()); case 'y' :return new Date((FullYear() + Number), Month(), Date(), Hours(), Minutes(), Seconds()); }
}
//+---------------------------------------------------
//| ⽐较⽇期差 dtEnd 格式为⽇期型或者有效⽇期格式字符串
//+---------------------------------------------------
Date.prototype.DateDiff = function(strInterval, dtEnd) {
var dtStart = this;
if (typeof dtEnd == 'string' )//如果是字符串转换为⽇期型
{
dtEnd = StringToDate(dtEnd);
}
switch (strInterval) {
case 's' :return parseInt((dtEnd - dtStart) / 1000);
case 'n' :return parseInt((dtEnd - dtStart) / 60000);
case 'h' :return parseInt((dtEnd - dtStart) / 3600000);
case 'd' :return parseInt((dtEnd - dtStart) / 86400000);
case 'w' :return parseInt((dtEnd - dtStart) / (86400000 * 7));
case 'm' :return (Month()+1)+((FullYear()-FullYear())*12) - (Month()+1);
case 'y' :FullYear() - FullYear();
}
}
//+---------------------------------------------------
//| ⽇期输出字符串,重载了系统的toString⽅法
//+---------------------------------------------------
String = function(showWeek)
{
var myDate= this;
var str = LocaleDateString();
if (showWeek)
{
var Week = ['⽇','⼀','⼆','三','四','五','六'];
str += ' 星期' + Day()];
}
return str;
}
//+---------------------------------------------------
//| ⽇期合法性验证
//| 格式为:YYYY-MM-DD或YYYY/MM/DD
//+---------------------------------------------------
function IsValidDate(DateStr)
{
var place(/(^\s+|\s+$)/g,''); //去两边空格;
if(sDate=='') return true;
//如果格式满⾜YYYY-(/)MM-(/)DD或YYYY-(/)M-(/)DD或YYYY-(/)M-(/)D或YYYY-(/)MM-(/)D就替换为'' //数据库中,合法⽇期可以是:YYYY-MM/DD(2003-3/21),数据库会⾃动转换为YYYY-MM-DD格式var s = place(/[\d]{ 4,4 }[\-/]{ 1 }[\d]{ 1,2 }[\-/]{ 1 }[\d]{ 1,2 }/g,'');
if (s=='') //说明格式满⾜YYYY-MM-DD或YYYY-M-DD或YYYY-M-D或YYYY-MM-D
{
var t=new place(/\-/g,'/'));
var ar = sDate.split(/[-/:]/);
if(ar[0] != t.getYear() || ar[1] != t.getMonth()+1 || ar[2] != t.getDate())
{
//alert('错误的⽇期格式!格式为:YYYY-MM-DD或YYYY/MM/DD。注意闰年。');
return false;
}
}
else
{
//alert('错误的⽇期格式!格式为:YYYY-MM-DD或YYYY/MM/DD。注意闰年。');
return false;
}
return true;
}
//+---------------------------------------------------
//| ⽇期时间检查
//| 格式为:YYYY-MM-DD HH:MM:SS
//+---------------------------------------------------
function CheckDateTime(str)
{
var reg = /^(\d+)-(\d{ 1,2 })-(\d{ 1,2 }) (\d{ 1,2 }):(\d{ 1,2 }):(\d{ 1,2 })$/;
var r = str.match(reg);
if(r==null)return false;
r[2]=r[2]-1;
var d= new Date(r[1],r[2],r[3],r[4],r[5],r[6]);
FullYear()!=r[1])return false;
Month()!=r[2])return false;
Date()!=r[3])return false;
Hours()!=r[4])return false;
Minutes()!=r[5])return false;
Seconds()!=r[6])return false;
return true;
}
//+---------------------------------------------------
//| 把⽇期分割成数组
/
/+---------------------------------------------------
Array = function()
{
var myDate = this;
var myArray = Array();
myArray[0] = FullYear();
myArray[1] = Month();
myArray[2] = Date();
myArray[3] = Hours();
myArray[4] = Minutes();
myArray[5] = Seconds();
jquery字符串截取return myArray;
}
//+---------------------------------------------------
//| 取得⽇期数据信息
//| 参数 interval 表⽰数据类型
//| y 年 m⽉ d⽇ w星期 ww周 h时 n分 s秒
//+---------------------------------------------------
Date.prototype.DatePart = function(interval)
{
var myDate = this;
var partStr='';
var Week = ['⽇','⼀','⼆','三','四','五','六'];
switch (interval)
{
case 'y' :partStr = FullYear();break;
case 'm' :partStr = Month()+1;break;
case 'd' :partStr = Date();break;
case 'w' :partStr = Day()];break;
case 'ww' :partStr = myDate.WeekNumOfYear();break;
case 'h' :partStr = Hours();break;
case 'n' :partStr = Minutes();break;
case 's' :partStr = Seconds();break;
}
return partStr;
}
//+---------------------------------------------------
//| 取得当前⽇期所在⽉的最⼤天数
//+---------------------------------------------------
Date.prototype.MaxDayOfDate = function()
{
var myDate = this;
var ary = Array();
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 = Array();
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 delimCahar = DateStr.indexOf('/')!=-1?'/':'-';
var arys= DateStr.split('-');
myDate = new Date(arys[0],--arys[1],arys[2]);
}
return myDate;
}
希望本⽂所述对⼤家JavaScript程序设计有所帮助。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论