Date型转换

1:Date对象转换成字符串

 1 // 对Date的扩展,将 Date 转化为指定格式的String
 2 // 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
 3 // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
 4 // 例子:
 5 // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
 6 // (new Date()).Format("yyyy-M-d h:m:s.S")   ==> 2006-7-2 8:9:4.18
 7 Date.prototype.Format = function (fmt) {
 8     var o = {
 9         "M+": this.getMonth() + 1, //月份
10         "d+": this.getDate(), //
11         "h+": this.getHours(), //小时
12         "m+": this.getMinutes(), //
13         "s+": this.getSeconds(), //
14         "q+": Math.floor((this.getMonth() + 3) / 3), //季度
15         "S": this.getMilliseconds() //毫秒
16     };
17     if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
18     for (var k in o)
19     if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
20     return fmt;
21 }
22 
23 var date1 = new Date().Format("yyyy-MM-dd");
24 var date2 = new Date().Format("yyyy-MM-dd hh:mm:ss");

2:字符串转换成Date对象

 1 //2.1方法一:输入的时间格式为yyyy-MM-dd
 2 function convertDateFromString1(dateString) {
 3     if (dateString) { 
 4         var date = new Date(dateString.replace(/-/,"/")) 
 5         return date;
 6     }
 7 }
 8 
 9 //2.2方法二:输入的时间格式为yyyy-MM-dd hh:mm:ss
10 function convertDateFromString2(dateString) { 
11     if (dateString) { 
12         var arr = dateString.replace(/ /g,"-").replace(/:/g,"-").split("-");
13         // 年月日时分秒
14         var date = new Date(arr[0], arr[1]-1, arr[2], arr[3], arr[4], arr[5]); 
15         return date;
16     } 
17 }

3:Date对象转换成时间戳

1 var newDate = new Date();
2 console.log(Number(newDate));

4:将时间戳对象转换成Date

1 var timestamp = 1523323795433;
2 var newDate = new Date(timestamp);
3 console.log(newDate);

5:new Date()的5种参数设定

 1 // month:用英文 表示月份名称,从January到December 
 2 // mth:  用整数表示月份,从(1月)到11(12月) 
 3 // dd:   表示一个 月中的第几天,从1到31 
 4 // yyyy: 四位数表示的年份 
 5 // hh:   小时数,从0(午夜)到23(晚11点) 
 6 // mm:   分钟数,从0到59的整数 
 7 // ss:   秒数,从0到59的整数 
 8 // ms:   毫秒数,为大于等于0的整数
 9 // 1)new Date("month dd,yyyy hh:mm:ss"); 
10 new Date("January 12,2006 22:19:35"); 
11 // 2)new Date("month dd,yyyy"); 
12 new Date("January 12,2006"); 
13 // 3)new Date(yyyy,mth,dd,hh,mm,ss); 
14 new Date(2006,0,12,22,19,35);
15 // 4)new Date(yyyy,mth,dd); 
16 new Date(2006,0,12); 
17 // 5)new Date(ms); 
18 new Date(1137075575000); 

6:表示星期

 

 1  // 6.1 方法一:直接判断
 2  var myDate = new Date();
 3  var year = myDate.getFullYear(); //获取完整的年份(4位,1970-????)
 4  var month = myDate.getMonth()+1; //获取当前月份(0-11,0代表1月)
 5  var date = myDate.getDate(); //获取当前日(1-31)
 6  var day = myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
 7  if(day==1){day="星期一";}
 8  if(day==2){day="星期二";}
 9  if(day==3){day="星期三";}
10  if(day==4){day="星期四";}
11  if(day==5){day="星期五";}
12  if(day==6){day="星期六";}
13  if(day==0){day="星期日";}
14  console.log(year + "-" + month + "-" + date + " " + day);
15  
16  // 6.2 方法二:对Date扩展
17  // (new Date()).pattern("yyyy-MM-dd E HH:mm:ss") ==> 2009-03-10 二 20:09:04
18  // (new Date()).pattern("yyyy-MM-dd EE hh:mm:ss") ==> 2009-03-10 周二 08:09:04
19  // (new Date()).pattern("yyyy-MM-dd EEE hh:mm:ss") ==> 2009-03-10 星期二 08:09:04
20  // (new Date()).pattern("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
21  Date.prototype.pattern=function(fmt) {
22      var o = {
23          "M+" : this.getMonth()+1, //月份
24          "d+" : this.getDate(), //
25          "h+" : this.getHours()%12 == 0 ? 12 : this.getHours()%12, //小时
26          "H+" : this.getHours(), //小时
27          "m+" : this.getMinutes(), //
28          "s+" : this.getSeconds(), //
29          "q+" : Math.floor((this.getMonth()+3)/3), //季度
30          "S" : this.getMilliseconds() //毫秒
31      };
32      // 把unicode解码成字符,要用转义字符'\u'
33      var week = {
34          "0" : "\u65e5",
35          "1" : "\u4e00",
36          "2" : "\u4e8c",
37          "3" : "\u4e09",
38          "4" : "\u56db",
39          "5" : "\u4e94",
40          "6" : "\u516d"
41      };
42      if(/(y+)/.test(fmt)){
43          fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
44      }
45      if(/(E+)/.test(fmt)){
46          fmt=fmt.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? "\u661f\u671f" : "\u5468") : "")+week[this.getDay()+""]);
47      }
48      for(var k in o){
49          if(new RegExp("("+ k +")").test(fmt)){
50              fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
51          }
52      }
53      return fmt;
54  }
55  
56  var date = new Date();      
57  console.log(date.pattern("yyyy-MM-dd EE hh:mm:ss"));

 

 

 

 7:计算两个时间差

 

 1 // 两个Date型日期直接相减,得出时间差(毫秒)
 2 parseInt(endTime.getTime() - startTime.getTime());
 3 
 4 // *如果需要算秒、分、时,需要对应再计算。
 5 var count = parseInt(inEndTime.getTime() - inStartTime.getTime());
 6 //相差天数
 7 var days=Math.floor(count/(24*3600*1000));
 8 //计算天数后剩余的毫秒数
 9 var leave1=count%(24*3600*1000);
10 //相差小时数
11 var hours=Math.floor(leave1/(3600*1000));
12 //计算小时数后剩余的毫秒数
13 var leave2=leave1%(3600*1000);
14 //相差分钟数
15 var minutes=Math.floor(leave2/(60*1000));
16 //计算分钟数后剩余的毫秒数
17 var leave3=leave2%(60*1000);
18 //相差秒数
19 var seconds=Math.round(leave3/1000);
20 
21 var result = days+"d "+hours+"h "+minutes+"m "+seconds+" s";

 

转载于:https://www.cnblogs.com/ciwei-214/p/8777114.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值