Js日期函数-Date方法

一.Date对象简介

1 使用时必须使用new来调用创建我们的日期对象(既构造对象)

构造当前时间对象主要有以下4中方法:
var MyDate = new Date()
var MyDate = new Date(milliseconds)
var MyDate = new Date(string)
var MyDate = new Date(y, m, d, h, min, sec, ms)

2 创建date的时候,传入要设置的时间参数

参数形式有以下5种:
参数格式(以2021年10月19日)
new Date(“month dd,yyyy hh:mm:ss”)new Date(“October 19,2021 11:05:35”)
new Date(“month dd,yyyy”)new Date(“October 19,2021”)
new Date(yyyy,mth,dd,hh,mm,ss)new Date(2021,9,19,11,09,35)
new Date(yyyy,mth,dd)new Date(2021,9,19)
new Date(ms)new Date(1634613216674)

返回的都是中国标准时间

参数简述

month:用英文 表示月份名称,从January到December
mth:用整数表示月份,从0(1月)到11(12月)
dd:表示一个 月中的第几天,从1到31
yyyy:四位数表示的年份
hh:小时数,从0(午夜)到23(晚11点)
mm: 分钟数,从0到59的整数
ss:秒数,从0到59的整数
ms:毫秒数,为大于等于0的整数

二.日期时间方法

使用new Date获取一个时间对象:var date = new Date()

1 时间对象

Date方法简介使用
date(中国标准时间)console.log(date);// Tue Oct 19 2021 11:37:44 GMT+0800 (中国标准时间)
date.toLocaleString()获取本地日期时间console.log(date.toLocaleString());// 2021/10/19 上午11:39:57
date.toLocaleDateString()获取本地日期console.log(date.toLocaleDateString());// 2021/10/19
date.toLocaleTimeString()获取本地时间console.log(date.toLocaleTimeString());// 上午11:42:12

2 时间的获取方法

Date方法简介使用
date.getDate()返回几号 (1 ~ 31)console.log(date.getDate());//19
date.getDay()返回星期几 (0 ~ 6),星期日为0console.log(date.getDay());//2
date.getMonth()返回月份 (0 ~ 11),从0开始console.log(date.getMonth());//9
date.getFullYear()返回四位数字的年份console.log(date.getFullYear());//2021
date.getYear()两位数的年份,从1900年开始console.log(date.getYear());//121
date.getHours()返回小时 (0 ~ 23)console.log(date.getHours());//22
date.getMinutes()返回分钟 (0 ~ 59)console.log(date.getMinutes());//54
date.getSeconds()返回秒 (0 ~ 59)console.log(date.getSeconds());//29
date.getMilliseconds()返回毫秒(0 ~ 999)console.log(date.getMilliseconds());//0
date.getTime()返回 1970 年 1 月 1 日至今的毫秒数console.log(date.getTime());//1634622998044

Date扩展

1 格式化日期

xxxx年xx月xx日星期x (2020年6月29日星期一)
 Date.prototype.format1st = function (fmt) {
    var arr = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
    var o = {
      "M+": this.getMonth() + 1 + '月', //月份
      "d+": this.getDate() + '日', //日
      "a+": arr[this.getDay()]
    };

    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;
  }
  console.log((new Date()).format1st("yyyyMda")); // 2021年10月19日星期二

xxxx-xx-xx xx:xx:xx (2020-01-01 08:00:07.523)

对Date的扩展,将 Date 转化为指定格式的String
月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)

为方便我们对日期(Date)进行格式化输出,先对 Date 进行扩展,增加 format 方法。以后调用 Date 对象的 format 方法即可将日期转换成我们指定格式的字符串(String)。
Date.prototype.format2nd = function (fmt) {
  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;
}
  // 2021-10-19 14:13:08.296
  console.log((new Date()).format2nd("yyyy-MM-dd hh:mm:ss.S"));
  // 2021-10-19 14:13:8.296
  console.log((new Date()).format2nd("yyyy-M-d h:m:s.S"));   
  console.log((new Date()).format2nd("yyyy-MM-dd hh:mm:ss"));   //2021-10-19 14:14:03  (常用)
  // 格式化输出指定时间
  var date = new Date("2021-10-19 14:13:8.296");
  console.log(date.format2nd("MM-dd hh:mm"));  // //10-19 14:13

注:RegExp

RegExp 是javascript中的一个内置对象。为正则表达式。
RegExp.$1是RegExp的一个属性,指的是与正则表达式匹配的第一个 子匹配(以括号为标志)字符串,以此类推,RegExp.$2,RegExp.$3,…RegExp.$99总共可以有99个匹配

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值