博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
小程序中时间比较的坑
阅读量:4095 次
发布时间:2019-05-25

本文共 669 字,大约阅读时间需要 2 分钟。

讲真这个微信小程序的坑真的是不少,在模拟器上啥问题没有的,到真机上会出现各种意想不到的问题。

场景还原

项目需要比较选中时间与当前时间,聪明如我当然想到了要把时间改成时间戳来比较辣,然鹅……

首先回顾一下js中获取时间戳的四种方法:

1. getTime()

var dates = new Date();var times = dates.getTime();//时间戳

2.valueOf()

var dates = new Date();var times = dates.valueOf();//时间戳

3. +new Date()

var dates1 = +new Date();//时间戳

4. Date.parse()

let time1 = Date.parse(new Date());

获取某一个时间的时间戳实例:

var dates = new Date("2030-08-18 08:08:08");var times = dates.getTime();

填坑

你高兴兴的把代码写完,模拟上也测试成功了。但是你会发现在真机上,获取某个时间的时间戳值为NaN。以上几种方法都试过了,都一样是NaN。

无意间发现,是因为某些机型是不支持“2021-02-20”这种格式的时间的。解决方案如下:

let data = e.detail.value; // 获取时间let time = new Date(data.replace(/-/g, '/'));time = Date.parse(time)

over!!!

转载地址:http://sovii.baihongyu.com/

你可能感兴趣的文章
Java编程基础:了解面向对象
查看>>
新一代Java模板引擎Thymeleaf
查看>>
Spring MVC中使用Thymeleaf模板引擎
查看>>
Spring Boot构建简单的微博应用
查看>>
Spring处理表单提交
查看>>
Spring MVC异常处理
查看>>
Leetcode 1180. Count Substrings with Only One Distinct Letter [Python]
查看>>
PHP 7 的五大新特性
查看>>
php使用 memcache 来存储 session
查看>>
php实现socket(转)
查看>>
PHP底层的运行机制与原理
查看>>
深入了解php底层机制
查看>>
PHP中的stdClass 【转】
查看>>
XHProf-php轻量级的性能分析工具
查看>>
PHP7新特性 What will be in PHP 7/PHPNG
查看>>
比较strtr, str_replace和preg_replace三个函数的效率
查看>>
ubuntu 下编译PHP5.5.7问题:configure: error: freetype.h not found.
查看>>
PHP编译configure时常见错误 debian centos
查看>>
configure: error: Please reinstall the BZip2 distribution
查看>>
OpenCV gpu模块样例注释:video_reader.cpp
查看>>