date函数
new date()
date 对象会自动把当前日期和时间保存为其初始值。
date.getdate()
从 date 对象返回一个月中的某一天 (1 ~ 31)。
date.getday()
从 date 对象返回一周中的某一天 (0 ~ 6)。周日是0。
date.getmonth()
从 date 对象返回月份 (0 ~ 11)。
date.getfullyear()
从 date 对象以四位数字返回年份
date.gethours()
返回 date 对象的小时 (0 ~ 23)。
date.getminutes()
返回 date 对象的分钟 (0 ~ 59)。
date.getseconds()
返回 date 对象的秒数 (0 ~ 59)。
date.getmilliseconds()
返回 date 对象的毫秒(0 ~ 999)。
date.gettime()
返回 1970 年 1 月 1 日至今的毫秒数。
date.gettimezoneoffset()
返回格林威治时间(gmt 或 utc)和本地时间之间的时差,以分钟为单位。返回之所以以分钟计,而不是以小时计,原因是某些
国家所占有的时区甚至不到一个小时的间隔。
date.getutcdate()
根据世界时从 date 对象返回月中的一天 (1 ~ 31)。
date.getutcday()
根据世界时从 date 对象返回周中的一天 (0 ~ 6)。周日是0。
date.getutcmonth()
根据世界时从 date 对象返回月份 (0 ~ 11)。
date.getutcfullyear()
根据世界时从 date 对象返回四位数的年份。
date.getutchours()
根据世界时返回 date 对象的小时 (0 ~ 23)。
date.getutcminutes()
根据世界时返回 date 对象的分钟 (0 ~ 59)。
date.getutcseconds()
根据世界时返回 date 对象的秒钟 (0 ~ 59)。
date.getutcmilliseconds()
根据世界时返回 date 对象的毫秒(0 ~ 999)。
date.parse(string)
返回1970年1月1日午夜到指定日期(字符串)的毫秒数。
date.setdate(int)
设置 date 对象中月的某一天 (1 ~ 31)。返回调整过的日期的毫秒表示。
date.setmonth(month[, day])
设置 date 对象中月份 (0 ~ 11)。day参数可选,表示该月的某一天介于 1 ~ 31 之间。
date.setfullyear(year[, month, day])
设置 date 对象中的年份(四位数字)。month参数可选,表示月份介于 0 ~ 11 之间。day参数可选,表示该月的某一天介于 1 ~ 31 之间。
date.sethours(hours[, minutes, seconds, milliseconds])
设置 date 对象中的小时 (0 ~ 23)。minutes参数可选,表示分钟数介于 0 ~ 59 之间。seconds参数可选,表示分钟数介于 0
~ 59 之间。milliseconds参数可选,表示分钟数介于 0 ~ 999 之间。
date.setminutes(minutes[, seconds, milliseconds])
设置 date 对象中的分钟 (0 ~ 59)。seconds参数可选,表示分钟数介于 0 ~ 59 之间。milliseconds参数可选,表示分钟数介于 0 ~ 999 之间。
date.setseconds(seconds[, milliseconds])
设置 date 对象中的秒钟 (0 ~ 59)。milliseconds参数可选,表示分钟数介于 0 ~ 999 之间。
date.setmilliseconds(int)
设置 date 对象中的毫秒 (0 ~ 999)。
date.settime(long)
设置的日期和时间据 gmt 时间 1970 年 1 月 1 日午夜之间的毫秒数。以毫秒形式表示日期可以使它独立于时区。
date.setutcdate(int)
根据世界时设置 date 对象中月份的一天 (1 ~ 31)。
date.setutcmonth(minutes[, seconds, milliseconds])
根据世界时设置 date 对象中的月份 (0 ~ 11)。seconds参数可选,表示分钟数介于 0 ~ 59 之间。milliseconds参数可选,表
示分钟数介于 0 ~ 999 之间。
date.setutcfullyear(year[, month, day])
根据世界时设置 date 对象中的年份(四位数字)。month参数可选,表示月份介于 0 ~ 11 之间。day参数可选,表示该月的
某一天介于 1 ~ 31 之间。
date.setutchours(hours[, minutes, seconds, milliseconds])
根据世界时设置 date 对象中的小时 (0 ~ 23)。minutes参数可选,表示分钟数介于 0 ~ 59 之间。seconds参数可选,表示分
钟数介于 0 ~ 59 之间。milliseconds参数可选,表示分钟数介于 0 ~ 999 之间。
date.setutcminutes(minutes[, seconds, milliseconds])
根据世界时设置 date 对象中的分钟 (0 ~ 59)。seconds参数可选,表示分钟数介于 0 ~ 59 之间。milliseconds参数可选,表
示分钟数介于 0 ~ 999 之间。
date.setutcseconds(seconds[, milliseconds])
根据世界时设置 date 对象中的秒钟 (0 ~ 59)。milliseconds参数可选,表示分钟数介于 0 ~ 999 之间。
date.setutcmilliseconds(int)
根据世界时设置 date 对象中的毫秒 (0 ~ 999)。
date.tostring()
把 date 对象转换为字符串。
date.totimestring()
把 date 对象的时间部分转换为字符串。
date.todatestring()
把 date 对象的日期部分转换为字符串。
date.toutcstring()
根据世界时,把 date 对象转换为字符串。
date.tolocalestring()
根据本地时间格式,把 date 对象转换为字符串。
date.tolocaletimestring()
根据本地时间格式,把 date 对象的时间部分转换为字符串。
date.tolocaledatestring()
根据本地时间格式,把 date 对象的日期部分转换为字符串。
date.utc(year,month,day[,hour,minute,second,milliseconds])
根据世界时返回 1970 年 1 月 1 日 到指定日期的毫秒数。年月日必填,时分秒毫秒都是选填的。
date.valueof()
返回 date 对象的原始值。与 date.gettime 返回的值相等。即 1970 年 1 月 1 日至今的毫秒数。
<html>
<head>
<style type="text/css">
body {
font-size:14px;
}
</style>
</head>
<body>
<script type="text/javascript">
function write(name, value) {
document.write(name + " = " + value + "<br/>");
}
var date = new date();//date 对象会自动把当前日期和时间保存为其初始值。
write("new date()", date);
write("date.getdate()", date.getdate());//从 date 对象返回一个月中的某一天 (1 ~ 31)。
write("date.getday()", date.getday());//从 date 对象返回一周中的某一天 (0 ~ 6)。周日是0。
write("date.getmonth()", date.getmonth());//从 date 对象返回月份 (0 ~ 11)。
write("date.getfullyear()", date.getfullyear());//从 date 对象以四位数字返回年份。
write("date.gethours()", date.gethours());//返回 date 对象的小时 (0 ~ 23)。
write("date.getminutes()", date.getminutes());//返回 date 对象的分钟 (0 ~ 59)。
write("date.getseconds()", date.getseconds());//返回 date 对象的秒数 (0 ~ 59)。
write("date.getmilliseconds()", date.getmilliseconds());//返回 date 对象的毫秒(0 ~ 999)。
write("date.gettime()", date.gettime());//返回 1970 年 1 月 1 日至今的毫秒数。
write("date.gettimezoneoffset()/60", date.gettimezoneoffset()/60);//返回格林威治时间(gmt 或 utc)和本地时间之间的时差,以分钟为单位。返回之所以以分钟计,而不是以小时计,原因是某些国家所占有的时区甚至不到一个小时的间隔。
write("date.getutcdate()", date.getutcdate());//根据世界时从 date 对象返回月中的一天 (1 ~ 31)。
write("date.getutcday()", date.getutcday());//根据世界时从 date 对象返回周中的一天 (0 ~ 6)。周日是0。
write("date.getutcmonth()", date.getutcmonth());//根据世界时从 date 对象返回月份 (0 ~ 11)。
write("date.getutcfullyear()", date.getutcfullyear());//根据世界时从 date 对象返回四位数的年份。
write("date.getutchours()", date.getutchours());//根据世界时返回 date 对象的小时 (0 ~ 23)。
write("date.getutcminutes()", date.getutcminutes());//根据世界时返回 date 对象的分钟 (0 ~ 59)。
write("date.getutcseconds()", date.getutcseconds());//根据世界时返回 date 对象的秒钟 (0 ~ 59)。
write("date.getutcmilliseconds()", date.getutcmilliseconds());//根据世界时返回 date 对象的毫秒(0 ~ 999)。
write("date.parse(string)", date.parse(date.tostring()));//返回1970年1月1日午夜到指定日期(字符串)的毫秒数。
write("date.setdate(int)", date.setdate(1));//设置 date 对象中月的某一天 (1 ~ 31)。返回调整过的日期的毫秒表示。
write("date.setmonth(int)", date.setmonth(0));//设置 date 对象中月份 (0 ~ 11)。
write("date.setfullyear(int)", date.setfullyear(2017));//设置 date 对象中的年份(四位数字)。
write("date.sethours(int)", date.sethours(0));//设置 date 对象中的小时 (0 ~ 23)。
write("date.setminutes(int)", date.setminutes(0));//设置 date 对象中的分钟 (0 ~ 59)。
write("date.setseconds(int)", date.setseconds(0));//设置 date 对象中的秒钟 (0 ~ 59)。
write("date.setmilliseconds(int)", date.setmilliseconds(0));//设置 date 对象中的毫秒 (0 ~ 999)。
write("date.settime(long)", date.settime(date.utc(2017,0,1)));//设置的日期和时间据 gmt 时间 1970 年 1 月 1 日午夜之间的毫秒数。以毫秒形式表示日期可以使它独立于时区。
write("date.setutcdate(int)", date.setutcdate(1));//根据世界时设置 date 对象中月份的一天 (1 ~ 31)。
write("date.setutcmonth(int)", date.setutcmonth(0));//根据世界时设置 date 对象中的月份 (0 ~ 11)。
write("date.setutcfullyear(int)", date.setutcfullyear(2017));//根据世界时设置 date 对象中的年份(四位数字)。
write("date.setutchours(int)", date.setutchours(0));//根据世界时设置 date 对象中的小时 (0 ~ 23)。
write("date.setutcminutes(int)", date.setutcminutes(0));//根据世界时设置 date 对象中的分钟 (0 ~ 59)。
write("date.setutcseconds(int)", date.setutcseconds(0));//根据世界时设置 date 对象中的秒钟 (0 ~ 59)。
write("date.setutcmilliseconds(long)", date.setutcmilliseconds(0));//根据世界时设置 date 对象中的毫秒 (0 ~ 999)。
write("date.tostring()", date.tostring());//把 date 对象转换为字符串。
write("date.totimestring()", date.totimestring());//把 date 对象的时间部分转换为字符串。
write("date.todatestring()", date.todatestring());//把 date 对象的日期部分转换为字符串。
write("date.toutcstring()", date.toutcstring());//根据世界时,把 date 对象转换为字符串。
write("date.tolocalestring()", date.tolocalestring());//根据本地时间格式,把 date 对象转换为字符串。
write("date.tolocaletimestring()", date.tolocaletimestring());//根据本地时间格式,把 date 对象的时间部分转换为字符串。
write("date.tolocaledatestring()", date.tolocaledatestring());//根据本地时间格式,把 date 对象的日期部分转换为字符串。
write("date.utc(year,month,day[,hour,minute,second,milliseconds])", date.utc(2017,0,1,0,0,0));//根据世界时返回 1970 年 1 月 1 日 到指定日期的毫秒数。年月日必填,时分秒毫秒都是选填的。
write("date.valueof()", date.valueof());//返回 date 对象的原始值。与 date.gettime 返回的值相等。即 1970 年 1 月 1 日至今的毫秒数。
</script>
</body>
</html>
格式化date
由于js本身并不提供格式化日期的函数。因此必须借助prototype属性来添加自定义的日期格式化方法。
<html>
<head>
<meta charset="utf8" />
</head>
<body>
<script type="text/javascript">
/*
年(y)可以用 1-4 个占位符
月(m)、日(d)、时(h,24时)、时(h,12时)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符
毫秒(s)只能用 1 个占位符(是 1-3 位数字)
am或pm只能用 1 个占位符(是 2 位英文)
上午或下午(t)只能用 1 个占位符(是 2 位中文)
星期(e)可以用 1-3 个占位符
季度(q)只能用 1 个占位符(是 1 位数字)
*/
date.prototype.format = function(fmt) {
var map = {
"m+" : this.getmonth() + 1, //月
"d+" : this.getdate(), //日
"h+" : this.gethours(), //24时
/*
上午12时只代表当天上午的12时,下午的12时代表当天下午的12时,
0时代表第二天的开始,即前面一天12时已过0时开始计算新一天的时间,
虽然说时间上跟前面那一天下午12时重合,但日期已经发生更改,所以不能说0时就是12时
*/
"h+" : this.gethours()%12 == 0 ? 12 : this.gethours()%12,//12时
"m+" : this.getminutes(), //分
"s+" : this.getseconds(), //秒
"s" : this.getmilliseconds(), //毫秒
"t" : this.gethours() < 12 ? "am" : "pm",
"t" : this.gethours() < 12 ? "上午" : "下午",
};
var week = {
"0" : "日",
"1" : "一",
"2" : "二",
"3" : "三",
"4" : "四",
"5" : "五",
"6" : "六",
}
var quarter = {
"0" : "一",
"1" : "二",
"2" : "三",
"3" : "四",
}
if(/(y+)/.test(fmt)) {
fmt = fmt.replace(regexp.$1, (this.getfullyear()+"").substr(4 - regexp.$1.length));
}
if(/(e+)/.test(fmt)) {
var weekprestr;
switch(regexp.$1.length) {
case 1:
weekprestr = "";
break;
case 2:
weekprestr = "周";
break;
default:
weekprestr = "星期";
break;
}
fmt = fmt.replace(regexp.$1, weekprestr + week[this.getday()]);
}
if(/(q)/.test(fmt)) {
fmt = fmt.replace(regexp.$1, quarter[math.floor(this.getmonth() / 3)]);
}
for(var key in map) {
if(new regexp("(" + key + ")").test(fmt)) {
fmt = fmt.replace(regexp.$1, regexp.$1.length == 1 ? map[key] : ("00" + map[key]).substr((map[key]+"").length));
}
}
return fmt;
}
function write(str) {
document.write(str + "<br/>");
}
var date = new date();
write(date.tostring());
write(date.toutcstring());
write(date.tolocalestring());
write("");
write(date.format("yy-m-d t h:m:s.s"));
write(date.format("yyyy-mm-dd hh:mm:ss.s"));
write(date.format("yyyy-mm-dd e hh:mm:ss"));
write(date.format("yyyy-mm-dd ee hh:mm:ss"));
write(date.format("yyyy-mm-dd eee hh:mm:ss"));
write(date.format("yyyy年mm月dd日 eee hh:mm:ss 第q季度"));
</script>
</body>
</html>
对date设置超出范围的参数
即使设置了超出范围的参数也不会报错,只会计算“最大合法值”与“超出值”相加得到的date。
<html>
<body>
<script type="text/javascript">
var date = new date();
date.setfullyear(2016,12,32, 0, 0, 0);
document.write(date);
</script>
</body>
</html>
比较date
<html>
<head>
<meta charset="utf8" />
</head>
<body>
<script type="text/javascript">
var date1 = new date();
var date2 = new date();
date1.settime(date.utc(2017,0,1));
date2.settime(date.utc(2016,11,31));
document.write("date1 = " + date1.tolocalestring() + "<br/>");
document.write("date2 = " + date2.tolocalestring() + "<br/>");
document.write("date1 - date2 = " + (date1 - date2) + "毫秒<br/>");
document.write("date1 - date2 = " + (date1 - date2)/1000/60/60 + "小时<br/>");
document.write("date1 > date2 = " + (date1 > date2) + "<br/>");
document.write("date1 < date2 = " + (date1 < date2) + "<br/>");
</script>
</body>
</html>
以上所述是小编给大家介绍的javascript date 知识浅析,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
更多javascript date 知识浅析。