js date(js new Date() 格式)
各位老铁们,大家好,今天由我来为大家分享js date,以及js new Date() 格式的相关问题知识,希望对大家有所帮助。如果可以帮助到大家,还望关注收藏下本站,您的支持是我们最大的动力,谢谢大家了哈,下面我们开始吧!
请教js中+newDate的作用
直接看实例吧。
var now= new Date();
alert(+now== now.getTime())
1.Date对象用于处理日期和时间。
创建 Date对象的语法:
var myDate=new Date()
Date对象会自动把当前日期和时间保存为其初始值。
2.参数形式有以下5种:
new Date("month dd,yyyy hh:mm:ss");
new Date("month dd,yyyy");
new Date(yyyy,mth,dd,hh,mm,ss);
new Date(yyyy,mth,dd);
new Date(ms);
最后一种形式,参数表示需要创建的时间和GMT时间1970年1月1日之间相差的毫秒数。
3.各种函数的含义如下:
month:用英文表示月份名称,从January到December
mth:用整数表示月份,从(1月)到11(12月)
dd:表示一个月中的第几天,从1到31
yyyy:四位数表示的年份
hh:小时数,从0(午夜)到23(晚11点)
mm:分钟数,从0到59的整数
ss:秒数,从0到59的整数
ms:毫秒数,为大于等于0的整数
如:
new Date("January 12,2006 22:19:35");
new Date("January 12,2006");
new Date(2006,0,12,22,19,35);
new Date(2006,0,12);
new Date(1137075575000);
Date()返回当日的日期和时间。
getDate()从 Date对象返回一个月中的某一天(1~ 31)。
getDay()从 Date对象返回一周中的某一天(0~ 6)。
getMonth()从 Date对象返回月份(0~ 11)。
getFullYear()从 Date对象以四位数字返回年份。
getYear()请使用 getFullYear()方法代替。
getHours()返回 Date对象的小时(0~ 23)。
getMinutes()返回 Date对象的分钟(0~ 59)。
getSeconds()返回 Date对象的秒数(0~ 59)。
getMilliseconds()返回 Date对象的毫秒(0~ 999)。
getTime()返回 1970年 1月 1日至今的毫秒数。
getTimezoneOffset()返回本地时间与格林威治标准时间(GMT)的分钟差。
getUTCDate()根据世界时从 Date对象返回月中的一天(1~ 31)。
getUTCDay()根据世界时从 Date对象返回周中的一天(0~ 6)。
getUTCMonth()根据世界时从 Date对象返回月份(0~ 11)。
getUTCFullYear()根据世界时从 Date对象返回四位数的年份。
getUTCHours()根据世界时返回 Date对象的小时(0~ 23)。
getUTCMinutes()根据世界时返回 Date对象的分钟(0~ 59)。
getUTCSeconds()根据世界时返回 Date对象的秒钟(0~ 59)。
getUTCMilliseconds()根据世界时返回 Date对象的毫秒(0~ 999)。
parse()返回1970年1月1日午夜到指定日期(字符串)的毫秒数。
setDate()设置 Date对象中月的某一天(1~ 31)。
setMonth()设置 Date对象中月份(0~ 11)。
setFullYear()设置 Date对象中的年份(四位数字)。
setYear()请使用 setFullYear()方法代替。
setHours()设置 Date对象中的小时(0~ 23)。
setMinutes()设置 Date对象中的分钟(0~ 59)。
setSeconds()设置 Date对象中的秒钟(0~ 59)。
setMilliseconds()设置 Date对象中的毫秒(0~ 999)。
setTime()以毫秒设置 Date对象。
setUTCDate()根据世界时设置 Date对象中月份的一天(1~ 31)。
setUTCMonth()根据世界时设置 Date对象中的月份(0~ 11)。
setUTCFullYear()根据世界时设置 Date对象中的年份(四位数字)。
setUTCHours()根据世界时设置 Date对象中的小时(0~ 23)。
setUTCMinutes()根据世界时设置 Date对象中的分钟(0~ 59)。
setUTCSeconds()根据世界时设置 Date对象中的秒钟(0~ 59)。
setUTCMilliseconds()根据世界时设置 Date对象中的毫秒(0~ 999)。
toSource()返回该对象的源代码。
toString()把 Date对象转换为字符串。
toTimeString()把 Date对象的时间部分转换为字符串。
toDateString()把 Date对象的日期部分转换为字符串。
toGMTString()请使用 toUTCString()方法代替。 1 3
toUTCString()根据世界时,把 Date对象转换为字符串。
toLocaleString()根据本地时间格式,把 Date对象转换为字符串。
toLocaleTimeString()根据本地时间格式,把 Date对象的时间部分转换为字符串。
toLocaleDateString()根据本地时间格式,把 Date对象的日期部分转换为字符串。
UTC()根据世界时返回 1997年 1月 1日到指定日期的毫秒数。
valueOf()返回 Date对象的原始值。
var objDate=new Date([arguments list]);
js new Date() 格式
对 new Date()得到日期的进行格式显示扩展,扩展方法如下:
Date.prototype.Format= function(fmt){//author: meizz
var o={
"M+": this.getMonth()+ 1,//月份
"d+": this.getDate(),//日
"h+": this.getHours(),//小时
"m+": this.getMinutes(),//分
"s+": this.getSeconds(),//秒
"q+": Math.floor((this.getMonth()+ 3)/ 3),//季度
"S": this.getMilliseconds()//毫秒
};
if(/(y+)/.test(fmt)) fmt= fmt.replace(RegExp.$1,(this.getFullYear()+"").substr(4-
RegExp.$1.length));
for(var k in o)
if(new RegExp("("+ k+")").test(fmt)) fmt= fmt.replace(RegExp.$1,(RegExp.$1.length== 1)?
(o[k]):(("00"+ o[k]).substr((""+ o[k]).length)));
return fmt;
}
使用例子:
(new Date()).Format("yyyy-MM-dd hh:mm:ss.S")//输出结果: 2017-01-23 09:36:10.400
(new Date()).Format("yyyy-M-d h:m:s.S")//输出结果: 2017-1-23 9:36:35.572
扩展资料
JS Date对象常用的带参数初始化方式:
1、用整数初始化日期对象
var date1= new Date(2017,06,06); console.log(date1);// Thu Jul 06 2017 00:00:00 GMT+0800(中国标准时间)
var date1= new Date(2017,1,1); console.log(date1);// Wed Feb 01 2017 00:00:00 GMT+0800(中国标准时间)
var date1= new Date(2017,01-2,01); console.log(date1);// Thu Dec 01 2016 00:00:00 GMT+0800(中国标准时间)
var date1=new Date(2017,06,06,06,06,06); console.log(date1);// Thu Jul 06 2017 06:06:06 GMT+0800(中国标准时间)
说明: new Date( year, month, date, hrs, min, sec)按给定的参数创建一日期对象
2、用字符串初始化日期对象
var date2= new Date(“2017/06/06”); console.log(date2);// Tue Jun 06 2017 00:00:00 GMT+0800(中国标准时间)
var date2= new Date(“2017-08-08”); console.log(date2);// Tue Aug 08 2017 08:00:00 GMT+0800(中国标准时间)
var date2= new Date(“2017-9-9”); console.log(date2);// Sat Sep 09 2017 00:00:00 GMT+0800(中国标准时间)
说明:如果字符串模式不支持短横杠模式,则进行字符串替换:
var strTime=”2011-04-16”;
var date2= new Date(Date.parse(strTime.replace(/-/g,“/”)));///-/g为正则表达式(RegExp)对象,表示全局替换-为/。
参考资料来源:百度百科- Date()
参考资料来源:百度百科- javascript
Js如何操作date对象
一共有十个常用的下面列举出来
1.new Date();这个也是必须的如果用到时间的话
2.getFullYear();获取当前年份
3.getMonth();获取当前的月份但是月份是从0-11算的所以获取到的月份需要+1
4.getHours();获取当前小时
5.getDate();获取当前日期
6.getMinutes();获取当前分钟
7.getSeconds();获取当前秒
8.getTime();获取当前毫秒值
9.toLocaleString();获取本地的时间格式字符串
10.getDay();获取当前星期外国星期日为第一天所以星期日为0其他的1-6一一对应
<scripttype="text/javascript">
//Date对象
/*1.newDate()获取当前时间
2.getFullYear()获取年份
3.getMonth()获取月份1月表示为0实际月份比显示月份大1
4.getHours()获取小时
5.getDate()获取日期
6.getMinutes()获取分钟
7.getSeconds()获取秒
8.getTime()获取毫秒值
9.toLocaleString()获取本地的时间格式字符串
10.getDay()获取星期外国星期日为第一天所以星期日为0星期六为6刚好对应*/
vardate=newDate();//空参构造显示当前时间
alert(date);
alert(date.getFullYear());
alert(date.getMonth());//计数时是从0开始算的到11
alert(date.getHours());
alert(date.getDate());
alert(date.getMinutes());
alert(date.getSeconds());
alert(date.getTime());
alert(date.toLocaleString());
alert(date.getDay());//星期计数0-6国外星期日是第一天所以星期日是0其他对应1-6
vardate2=newDate(10000000000000);//里面可以加参数参数是以毫秒为单位的起始年月从格林尼治时间开始算1970年一月一日
alert(date2.toLocaleString());
</script>
感谢您花时间阅读本文!我们希望通过对js date和js new Date() 格式的问题进行探讨,为您提供了一些有用的见解和解决方案。如果您需要更多帮助或者有其他疑问,请不要犹豫与我们联系。