当前位置: 首页 > news >正文

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.296console.log((new Date()).format2nd("yyyy-MM-dd hh:mm:ss.S"));// 2021-10-19 14:13:8.296console.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个匹配

http://www.xdnf.cn/news/824185.html

相关文章:

  • 颜表情记录(自用
  • extjs Ext.data.Store store学习
  • 什么是802.11ac和802.11ac Wave2
  • 【模拟集成电路】鉴频鉴相器设计(Phase Frequency Detector,PFD)
  • think PHP之环境配置windows+Wamp+Composer+PHP
  • 2024年网络安全最全网工必备工具SecureCRT_crt工具(1),瞬间高大上了
  • 数据流图(DFD)
  • 数据归档与清理功能大幅升级,NineData重磅升级!
  • CSS中实现元素居中的七种方法
  • iOS 苹果授权登录(Sign in with Apple)系列之Apple Developer配置篇
  • 放弃Python拥抱Mojo?鹅厂工程师真实使用感受
  • 推荐几个好用实用的免费图标素材(好看的icon)
  • 开窗函数(分析函数)使用详解
  • 最新配置淘宝镜像的方法
  • Ubuntu安装和配置ssh教程
  • document.getElementById()方法使用
  • equalsIgnoreCase() 方法
  • 【软件开发流程】
  • OSPF协议详解
  • B/S结构和C/S结构详细介绍
  • HDU之算法初步
  • disruptor原理详解
  • 网安学途—SQL SERVER 2008安装教程
  • Apache Log4j2 详解 (一)
  • C语言——动态内存函数(malloc、calloc、realloc、free)
  • 【揭秘】ScheduledExecutorService全面解析
  • postfix(邮件服务器)说明与postconfig命令详解
  • 2025年5月TIOBE 指数头条:Python 统治世界!多家权威机构____编程语言排行榜__薪酬状况
  • User-Agent(用户代理)是什么?
  • CentOS7保姆级安装教程