首页 > 编程语言 >javascript parse date string - js 字符串转日期

javascript parse date string - js 字符串转日期

时间:2022-10-10 11:36:16浏览次数:85  
标签:10 string UTC javascript js parse date time Date

一、日期数字

new Date().getTime() // 1665370859678

数字表示从 UTC+0 时区的1970年1月1日0时0分0秒开始的那一刻起,所经过的毫秒数。
无论是在北京还是伦敦,此时此刻,无论本地时间是几点,它们经过的毫秒数都是一样的。

二、日期字符串

字符串根数字不一样,字符串是被时区化了的结果。
比如: 2022-10-08 这个字符串,在中国和伦敦,所表示的经过的毫秒数的时间是不一样的。

三、UTC 时间

UTC 相当于本初子午线(即经度0度)上的平均太阳时,过去曾用格林威治平均时(GMT)来表示。
GMT 格林尼治标准时间(Greenwich Mean Time)是指位于伦敦郊区的皇家格林尼治天文台的标准时间,因为本初子午线被定义在通过那里的经线。
所以 UTC 时间也是有时区的,它的时区为 0 时区。

四、字符串转日期

在将字符串转为 Date 对象时,需要指定一个时区,才能准确的表示时间。
(注:Date 对象也是一个没有时区限制的对象。只不过它有默认的本地时区,通常转换成字符时间表示时,默认自动转换成本地时区的时间。)
如果不指定时区, Date 对象会默认指定一个字符串的时区(根据字符串格式的不同)

When the time zone offset is absent, date-only forms are interpreted as a UTC time and date-time forms are interpreted as local time.

/*
 * 当:不指定时区解析字符串时的:默认时区
 */

// 以下:默认为 0 时区的日期
Date.parse("2011-10-10")   // date-only 只有日期

// 以下:默认为 本地时区的日期 
// 所以本地电脑时区为北京时间和伦敦时间时,解析出的日期是不一样的。
Date.parse("2011-10-10T14:48:00")   // date-time 日期+时间
Date.parse("10/10/2022")            // Non-standard date strings 非标准日期格式
Date.parse("October 10, 2014")      // Non-standard date strings 非标准日期格式
Date.parse("Oct 10, 2014")          // Non-standard date strings 非标准日期格式

五、解析字符串时如何指定时区

/*
 * 在字符串后面添加: GMT (或 UTC) 
 */ 

Date.parse('2011-10-10 GMT-0300')  // 西三区
Date.parse('2011-10-10 UTC-0300')  // 西三区

Date.parse('2011-10-10 UTC+0800')  // 东八区

Date.parse('2011-10-10 GMT')       // 0时区
Date.parse('2011-10-10 UTC')       // 0时区
Date.parse('2011-10-10 UTC+0000')  // 0时区

附:标准日期格式

21.4.1.18 Date Time String Format
ECMAScript defines a string interchange format for date-times based upon a simplification of the ISO 8601 calendar date extended format. The format is as follows: YYYY-MM-DDTHH:mm:ss.sssZ

Where the elements are as follows:

YYYY	is the year in the proleptic Gregorian calendar as four decimal digits from 0000 to 9999, or as an expanded year of "+" or "-" followed by six decimal digits.
-	"-" (hyphen) appears literally twice in the string.
MM	is the month of the year as two decimal digits from 01 (January) to 12 (December).
DD	is the day of the month as two decimal digits from 01 to 31.
T	"T" appears literally in the string, to indicate the beginning of the time element.
HH	is the number of complete hours that have passed since midnight as two decimal digits from 00 to 24.
:	":" (colon) appears literally twice in the string.
mm	is the number of complete minutes since the start of the hour as two decimal digits from 00 to 59.
ss	is the number of complete seconds since the start of the minute as two decimal digits from 00 to 59.
.	"." (dot) appears literally in the string.
sss	is the number of complete milliseconds since the start of the second as three decimal digits.
Z	is the UTC offset representation specified as "Z" (for UTC with no offset) or as either "+" or "-" followed by a time expression HH:mm (a subset of the time zone offset string format for indicating local time ahead of or behind UTC, respectively)
This format includes date-only forms:

YYYY
YYYY-MM
YYYY-MM-DD
        
It also includes “date-time” forms that consist of one of the above date-only forms immediately followed by one of the following time forms with an optional UTC offset representation appended:

THH:mm
THH:mm:ss
THH:mm:ss.sss
        

ref:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
https://tc39.es/ecma262/#sec-date-time-string-format

标签:10,string,UTC,javascript,js,parse,date,time,Date
From: https://www.cnblogs.com/eddyz/p/16775034.html

相关文章

  • Vue系列---【at Socket.writeAfterFIN [as write] (net.js:441:14) at PoolWorker
    1.问题描述:前端是vue项目,打包和打镜像的时候,本地没问题,jenkins物理机打流水线也没问题,但是到容器云平台使用自带的流水线打包打镜像的时候,就报错了。上次成功上线的代......
  • js闭包理解
    js闭包其实就是一句话闭包变量就是函数对象的属性例1functionf1(){varn=999;functionf2(){n++;alert(n);}returnf2;}varresult=f1();result();r......
  • 判断某个js文件是否已存在
    isScriptAlready(src:string){    constfound=      performance        .getEntries()        .filter((......
  • 关于对JS-面向对象-的理解
    最近看了《你不知道的JS上卷》这本书,写下自己的一点感悟!类首先,关于类,这是一种设计模式。JS是一门真正面对对象的语言为什么这样说呢?像JAVA这种众所周知的面对对象的语......
  • jsp操作本地数据库
    @目录环境准备检查驱动下载驱动配置Tomcat配置idea开始上手测试连接主界面显示列出全部学生模块实现条件查询学生模块实现添加学生模块实现条件删除学生模块实现条件修改学......
  • Uncaught TypeError: Converting circular structure to JSON
     在使用JSON.stringify方法去转化成字符串,会报错TypeError:ConvertingcircularstructuretoJSON原因: 对象中有对自身的循环引用; 解决方法:下面的 json_str 就......
  • 时间戳转换成日期格式,格式日期转换成时间戳 js
    格式化时间戳1timestampToTime(timestamp){2constdt=newDate(timestamp*1000)3consty=dt.getFullYear()4......
  • js异步编程面试题你能答上来几道
    在上一节中我们了解了常见的es6语法的一些知识点。这一章节我们将会学习异步编程这一块内容,鉴于异步编程是js中至关重要的内容,所以我们将会用三个章节来学习异步编程涉及到......
  • JS模块化—CJS&AMD&CMD&ES6-前端面试知识点查漏补缺
    本文从以时间为轴从以下几个方面进行总结JS模块化。从无模块化=>IIFE=>CJS=>AMD=>CMD=>ES6=>webpack这几个阶段进行分析。历史幼年期:无模块化方式需要......
  • JS 中的垃圾回收方式
    1.什么是垃圾(1)没有被引用的对象或变量(2)无法访问到的对象(几个对象引用形成一个环,互相引用)可达性是指那些以某种方式可以访问到或可以用到的值,它们被保证存储在内存中。......