首页 > 语言 > JavaScript > 正文

javascript中Date对象的使用总结

2024-05-06 15:06:34
字体:
来源:转载
供稿:网友

JSON 日期转 JS日期,我们知道,日期类型转成JSON之后,返回的数据类似这样:

  /Date(1379944571737)/

  但是这种日期并不能直接显示,因为根本没有人知道这是什么意思,下面提供一种JSON日期转JS日期的方式。

function ConvertJSONDateToJSDate(jsondate) { var date = new Date(parseInt(jsondate.replace("/Date(", "").replace(")/", ""), 10)); return date;}

在提供两种Date转习惯视觉的日期格式:

//yyyy-MM-ddfunction getDate(date) { var year = date.getFullYear(); var month = date.getMonth() + 1; var day = date.getDate(); return year + "-" + month + "-" + day ;}//yyyy-MM-dd HH:mm:SSfunction getDateTime(date) { var year = date.getFullYear(); var month = date.getMonth() + 1; var day = date.getDate(); var hh = date.getHours(); var mm = date.getMinutes(); var ss = date.getSeconds(); return year + "-" + month + "-" + day + " " + hh + ":" + mm + ":" + ss;}

将一个字符串转换为Date对象的写法:

 var str = "2012-12-12"; var date = new Date(str);  //字符串转换为Date对象 document.write(date.getFullYear());  //然后就可以使用Date对象的方法输出年份了

一、Date.getDate()  返回是日期对象中月份中的几号。

 var date = new Date();  //2012-12-19 document.write(date.getDate());  //返回 19 是19号

二、Date.getDay()  返回日期中的星期几  星期天0-星期6

 var date = new Date(); document.write(date.getDay());  //3 星期3

三、Date.getFulYead()  返回年份  如2012。

 var date = new Date(); document.write(date.getFullYear());  //返回2012,2012年

四、Date.getHours()  返回日期中的小时,几点了,0-23

 var date = new Date(); document.write(date.getHours());  //返回23,晚上11点

五、Date.getMilliseconds()  返回日期中的毫秒数

 var date = new Date(); document.write(date.getMilliseconds());  //返回27  当前是xx年,xx月,xx点,xx分,xx秒,xx毫秒的毫秒

六、Date.getMinutes()    返回日期中的分钟数  0-59

 var date = new Date(); document.write(date.getMinutes());  //2012-12-19 23:22  返回22,12点22分

七、Date.getMonth()      //返回日期中的月份数,返回值0(1月)-11(12月)

 var date = new Date(); document.write(date.getMonth());  //2012-12-19  此处返回11,注意此处与通常理解有些偏差,1月份返回是0,12月返回是11

八、Date.getSeconds()    //返回一个日期的描述

 var date = new Date(); document.write(date.getSeconds());·//返回34,2012-12-19 23:27:34  27分34秒            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选