Date 对象,是操作日期和时间的对象。

Date 为内置的构造函数, 通过 new Date () 来获取当前本地日期与时间

const time = new Date
console.log(time )//Mon Nov 05 2018 15:20:49 GMT+0800

  传参

传递Number 将从 '1970/01/01 00:00:00' 为起点,开始叠加的毫秒数,传入负值将倒退。

time(1000)//Thu Jan 01 1970 08:00:01 GMT+0800 (中国标准时间)
time(-1000)//Thu Jan 01 1970 07:59:59 GMT+0800 (中国标准时间)

传递日期格式字符串 直接显示对应日期时间

  字符串的格式主要有两种:

  1. yyyy/MM/dd HH:mm:ss (推荐):若省略时间,返回的 Date 对象的时间为 00:00:00。
  2. yyyy-MM-dd HH:mm:ss :若省略时间,返回的 Date 对象的时间为 08:00:00(加上本地时区)。此字符串在IE中会转换失败!
time('2018-12-31')//Mon Dec 31 2018 08:00:00 GMT+0800 (中国标准时间)
time('2018/12/1 12:00:00')//Sat Dec 01 2018 12:00:00 GMT+0800 (中国标准时间)

方法

Date 对象的方法主要分为2种形式:本地时间和 UTC 时间。

  • getFullYear() :返回四位数字年份。

  • getMonth() :返回月份值。从0开始,所以真实月份=返回值+1 。

  • getDate() :返回月份中的日期值;值的范围1~31 。

  • getHours() :返回小时值(0 ~ 23)。

  • getMinutes() :返回分钟值(0 ~ 59)。

  • getSeconds() :返回秒数值 (0 ~ 59)。

  • getMilliseconds() :返回毫秒值(0 ~ 999)。

  • getDay() :返回星期值;0为星期天,1为星期一、2为星期二,依此类推

  • getTime() :返回 Date 对象与'1970/01/01 00:00:00'之间的毫秒值(北京时间的时区为东8区,起点时间实际为:'1970/01/01 08:00:00') 。
  • toLocaleString() :根据本地时间把 Date 对象转换为字符串,并返回结果。
  • toLocaleDateString():方法可根据本地时间把 Date 对象的日期部分转换为字符串。
  • getTimezoneOffset() :返回格林威治时间和本地时间之间的时差,以分钟为单位。
  • toLocaleTimeString(): 方法可根据本地时间把 Date 对象的时间部分转换为字符串。
  • valueOf() 方法返回 Date 对象的原始值。
new Date().getFullYear()//2018 年
new Date().getMonth()//10 月 (月份+1 实际为11)
new Date().getDate()//5 日
new Date().getHours()//16 时
new Date().getMinutes()//2 分
new Date().getSeconds()//58 秒
new Date().getMilliseconds()//940 毫秒
new Date().getDay()//1 //星期
new Date().getTime()//1541405023106 // 从 '1970/01/01 00:00:00' 到该时间的毫秒
new Date().getTimezoneOffset()/60 //-8 格林威治时间与本地时间差
new Date(1543636820000).toLocaleString() //"2018/12/1 下午12:00:20" 格式化
new Date('2018/12/1 12:00:00').valueOf()//1543636800000 与getTime方法类似
new Date('2018/12/1 12:00:00').toLocaleDateString()//"2018/12/1" 日期格式化
new Date('2018/12/1 12:00:00').toLocaleTimeString()//"下午12:00:00" 时间格式

静态方法

Date.now()
//无参数 获取当前时间以毫秒形式返回与getTime()类似 区别:不需要 new Date
Date.parse('2018')
// 参数: 日期格式的字符串 将日期转化为毫秒 以'1970/01/01 08:00:00'开始 (格林时间+本地时区) 注意!!! 后3位将置 0
//1514764800000

+ new Date() 可以返回毫秒数 与getTime 方法类似

Date 对象 时间格式注意事项的更多相关文章

  1. 微信小程序中new Date()转换时间时间格式时IOS不兼容的问题

    本周写小程序,遇到的一个bug,在chrome上显示得好好的时间,一到Safari/iPhone 就报错 “invalid date”,时间格式为“2019.06.06 13:12:49”,然后利用n ...

  2. 扩展JS Date对象时间格式化功能

    在自己JS代码中引入一下代码: Date.prototype.format =function(format) { var o = { "M+" : this.getMonth() ...

  3. toLocaleTimeString()方法在IE和谷歌浏览器上 根据本地时间格式,把 Date 对象的时间部分(不含日期)转换为“时间字符串”存在区别

    这两天修改一个bug,发现一个问题:  toLocaleTimeString()方法在IE和谷歌浏览器上 根据本地时间格式,把 Date 对象的时间部分(不含日期)转换为“时间字符串”存在区别.方法原 ...

  4. javascript Date对象 之 时间转字符串

    javascript Date对象 --> 时间转字符串: 测试代码: <!DOCTYPE html> <html lang="en"> <he ...

  5. NSdate 时间格式

    NSdate 时间格式 NSTimeInterval 时间间隔 基本单位 秒 NSDateFormatter 时间格式器 用于日期对象的格式化或字符串解析为日期对象 日期格式如下: y  年 M  年 ...

  6. JS的Date对象、Math、包装类

    Date对象 在JS使用Date对象来表示时间  当前时间 var d = new Date();  指定时间 格式:月/日/年 时:分:秒 var e = new Date("02/16/ ...

  7. js Date对象总结

    Date在js中和Array类似,都是拥有自己的特殊方法的特殊对象. 由于平常用到Date着实不多,对它的了解颇浅.上周被问到怎么样获取某年某个月的天数,我当时想了一会儿,回答说有两种,一种自己写判断 ...

  8. mysql 时间格式与日期格式转换,去除datetime中的具体时间

    DATE_FORMAT(`addtime`,'%Y-%m-%d')  时间格式转成字符串 time_format('1924-01-02', '%Y-%m-%d') 字符串转成时间格式 CONVERT ...

  9. JS时间格式和时间戳的相互转换

    时间戳转化为日期的方式 ; var newDate = new Date(); newDate.setTime(timestamp * ); // Mon May 28 2018 console.lo ...

随机推荐

  1. BZOJ3524:[POI2014]Couriers

    浅谈主席树:https://www.cnblogs.com/AKMer/p/9956734.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem.p ...

  2. C# 序列化反序列化XML的帮助类

    以下是一个包装的用于序列化反序列化XML和C# 对象的类.  public class XmlSerializeHelper<T>     {         #region Serial ...

  3. cnblogs开源合集

    http://www.cnblogs.com/davytitan/p/4135151.html

  4. 如何用CSS实现矩形按钮右边缘的中间有个往里凹的小半圆

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...

  5. linux 下消息队列发送后没有信息

    在使用消息队列时,调用 #include <stdio.h> #include <stdlib.h> #include <string.h> #include &l ...

  6. Google Analytics添加到网站

    把Google Analytics添加到网站的具体方式 https://developers.google.com/analytics/devguides/collection/analyticsjs ...

  7. QDUOJ 河老师的新年礼物(尺取法)

    河老师的新年礼物 发布时间: 2017年1月1日 15:11   最后更新: 2017年1月1日 15:13   时间限制: 1000ms   内存限制: 256M 描述 河老师的新年礼物是一个长度为 ...

  8. 【转】ssm整合

    http://m.blog.csdn.net/article/details?id=44455235 SSM框架——详细整合教程(Spring+SpringMVC+MyBatis) 发表于2015/3 ...

  9. MCP|BFY|Proteome Analysis of Human Neutrophil Granulocytes From Patients With Monogenic Disease Using Data-independent Acquisition(单基因疾病患者中性粒细胞的DIA蛋白质组分析)

    文献名:Proteome Analysis of Human Neutrophil Granulocytes From Patients With Monogenic Disease Using Da ...

  10. 剑指Offer的学习笔记(C#篇)-- 从上往下打印二叉树

    题目描述 从上往下打印出二叉树的每个节点,同层节点从左至右打印. 一 . 题目解析 了解过二叉树就应该知道,二叉树存在三种遍历方法:前序遍历(根→左→右).中序遍历(左→根→右).后续遍历(左→右→根 ...