在做项目的时候,无意发现了一个小东西。

new Date('2018-05-15')

new Date('2018-5-15')

输出的结果是不同的,相差了8小时。然后让我回忆到之前看的一个时间转换函数,把-替换成/。于是,我把它替换了一下。问题就解决了,返回的就是相同的时间。所以可以简单地得出一个结论:

  1. 其实不关是否加了0的问题
  2. 把-替换成/,可以躲过这个坑

本来这个日记已经完结,我没死心又跑去MDN找了Date对象的知识。其中有一个写着new Date(dateString),接着,我们看到:该字符串(dateString)应该能被 Date.parse() 方法识别。再翻到这个parse的文档,里面就写着类似(2015-02-31)这种属于非法的格式。这也就正式完结了,这是一个超级基础的知识啊。

接着还有后续,热心网友提供了一个其他信息

> new Date("2012/03/13")
Tue Mar 13 2012 00:00:00 GMT-0400 (EDT)
This parses to midnight in the local time zone. This behavior is universal across browsers. > new Date("2012-03-13")
Mon Mar 12 2012 20:00:00 GMT-0400 (EDT)
This can be interpreted as an ISO-8601 date, and so it is (following our last rule of thumb). This means that it is parsed as UTC. But it is displayed using local time. I'm in the EDT time zone, hence the difference of four hours. This format is only supported by newer browsers (Chrome, FF4+, IE9). > new Date("2012-3-13")
Tue Mar 13 2012 00:00:00 GMT-0400 (EDT)
This cannot be interpreted as an ISO-8601 date, since it's missing a "0" in front of the month ("3" vs. "03"). So Chrome falls back to interpreting this as a local time. At the moment, Chrome is the only browser which supports this particular gem of a format. A final cautionary note: if you are writing a library and wish to convert date strings to millis since epoch, the built-in Date.parse method looks like just the ticket. But you'll eventually run into an issue. For reasons known only to its authors, MooTools overrides this function with an incompatible version (theirs returns a Date, not a number). What you really want is new Date(dateString).getTime().

JS时间转换的一个坑位的更多相关文章

  1. JS 时间转换函数 字符串时间转换毫秒(互转)

    字符串转化为日期 let util = function(){ Date.prototype.Format = function(fmt) { var o = { "M+" : t ...

  2. JS时间转换,url编码,jquery返回类型等问题

    1.当时间被转换为json格式后会被转换成 /Date(...)/ 这种格式,其中...为时间转换成妙后的一串整数 function changeDateFormat(cellval) { )); v ...

  3. js 时间转换

    //时间转换成时间戳 function DateToUnix(string) { var f = string.split(' ', 2); var d = (f[0] ? f[0] : '').sp ...

  4. js时间转换

    1. 将时间戳转换成日期格式 // 简单的一句代码 var date = new Date(时间戳); //获取一个时间对象 /** 1. 下面是获取时间日期的方法,需要什么样的格式自己拼接起来就好了 ...

  5. js时间转换相关

    1.json时间格式转换 function ChangeDateFormat(jsondate) { if (!jsondate||jsondate.length < 1) {return &q ...

  6. Java时间转换的一个特性

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); Date codedat ...

  7. js 时间转换毫秒的四种方法(转)

    将时间转换为毫秒数的方法有四个: Date.parse()Date.UTCvalueOf()getTime() 1. Date.parse():该方法接受一个表示日期的字符串参数,然后尝试根据这个日期 ...

  8. Linq 时间参数的一个坑

    背景:查询某个字段大于系统时间的数据 两种写法: 1.DataTime now=DateTime.Now; var result=dbContext.Table1.Created>now 2.  ...

  9. 从angularJS改道Vue.js,趟过第一个坑!

    vue采用 new vue()初始化,显然vue内部没有类似jquery ready函数的机制,在文档加载完成后再执行初始化. 今天新学习vue,由于vue采用es5的特殊机制更新UI,我不确定ipa ...

随机推荐

  1. bzoj5127[Lydsy12月赛]数据校验

    多少年不写题解了 题目描述: 著名出题人小 Q 出了一道题,这个题给定一个正整数序列 a1, a2, ..., an,并保证输入数据中,对于 a 的任意一个非空连续子区间 [l, r],该区间内出现过 ...

  2. Django获取多个数据及文件上传

    1. 选择性别: 1)更新login.html模板 2)views.py中获取客户端提交的数据 3)当选择性别为“男”后,后台得到了gender=1. 2.选择爱好,可以多选,所以后台会获得多个数据. ...

  3. elasticsearch 第五篇(文档操作接口)

    INDEX API 示例: 1 2 3 4 5 PUT /test/user/1 { "name": "silence", "age": 2 ...

  4. HSTS的来龙去脉

    前言 安全经常说“云.管.端”,“管”指的是管道,传输过程中的安全.为了确保信息在网络传输层的安全,现在很多网站都开启了HTTPS,也就是HTTP+TLS,在传输过程中对信息进行加密.HTTPS使用了 ...

  5. C内存对齐问题-bus error!总线错误!其实是 字符串字面量修改问题!

    最近写个小程序,出现bus error! int main(void) { /** * char :1个字节 * char*(即指针变量): 4个字节(32位的寻址空间是2^32, 即32个bit,也 ...

  6. Ambari和ClouderaManager主要不同对比

    打算对新建的hadoop集群使用管理工具,列了以下主要的不同点: 主要的不同点 apache Ambari ClouderaManager Express(免费版) 配置版本控制和历史记录 支持 不支 ...

  7. Python/spss-多元回归建模-共线性诊断2(推荐AA)

    sklearn实战-乳腺癌细胞数据挖掘(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&a ...

  8. Java常量池详解之Integer缓存

    一个Java question,求输出结果   public class IntegerTest { public static void main(String[] args) { objPoolT ...

  9. (转) 使用vivado创建工程 3

    Create a Hello World application In this experiment we will use Xilinx SDK to create a simple Hello ...

  10. Android 统一配置依赖管理

    Android Studio中默认就是使用Gradle来构建管理工程的,当我们在工程构建过程中创建了多个Module时,就可能存在一个问题,那就是每个Module以及Module中一些公用库的依赖存在 ...