微信小程序时间戳,系统时间存储解决方案

微信小程序时间戳,系统时间存储解决方案

什么是时间戳?

科普:(来自百度)
时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数。

时间存储方案

(个人看法,不足之处还望指出)
需要存储时间时,将时间转换成时间戳存储,优点:方便对时间的计算以及比较,也可再转换成显示的时间。

时间转换为时间戳

1、时间转换成时间戳(格式为:年-月-日 时:分:秒)

//日期 时间转换为时间戳
 var starttime = new Date(("2019-05-10").replace(/-/g, "/")).getTime() / 1000
 //或
  var starttime = new Date(("2019-05-10 11:54:12").replace(/-/g, "/")).getTime() / 1000

时间戳转换为时间

以下代码原文(感谢:UFO00001)
2、获取系统当前时间戳以及将时间戳转换为日期和时间(见代码)

//获取当前时间戳
    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;
    console.log("当前时间戳为:" + timestamp);
 
//获取当前时间
    var n = timestamp * 1000;
    var date = new Date(n);
    //年
    var Y = date.getFullYear();
    //月
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    //日
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    //时
    var h = date.getHours();
    //分
    var m = date.getMinutes();
    //秒
    var s = date.getSeconds();
  
    console.log("当前时间:" +Y+M+D+h+":"+m+":"+s);
    
//转换为时间格式字符串
    console.log(date.toDateString());
 
    console.log(date.toGMTString());
 
    console.log(date.toISOString());
  
    console.log(date.toJSON());
    
    console.log(date.toLocaleDateString());
 
    console.log(date.toLocaleString());
  
    console.log(date.toLocaleTimeString());
   
    console.log(date.toString());
   
    console.log(date.toTimeString());
   
    console.log(date.toUTCString());
//时间、时间戳加减 以加一天举例,聪明的你肯定触类旁通
    //加一天的时间戳:
    var tomorrow_timetamp = timestamp + 24 * 60 * 60;
    //加一天的时间:
    var n_to = tomorrow_timetamp * 1000;
    var tomorrow_date = new Date(n_to);
    //加一天后的年份
    var Y_tomorrow = tomorrow_date.getFullYear();
    //加一天后的月份
    var M_tomorrow = (tomorrow_date.getMonth() + 1 < 10 ? '0' + (tomorrow_date.getMonth() + 1) : tomorrow_date.getMonth() + 1);
    //加一天后的日期
    var D_tomorrow = tomorrow_date.getDate() < 10 ? '0' + tomorrow_date.getDate() : tomorrow_date.getDate();
    //加一天后的时刻
    var h_tomorrow = tomorrow_date.getHours();
    //加一天后的分钟
    var m_tomorrow = tomorrow_date.getMinutes();
    //加一天后的秒数
    var s_tomorrow = tomorrow_date.getSeconds();

记录以便日后查询(时间:2019年5月10日)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值