• 时间戳转日期


    时间戳转日期

    做过日期的小伙伴可能都知道,一般的日期组件,都不怎么符合自己的需求~~

    下面是一个时间戳转日期的封装函数. , 有了这个函数我们可以方便的,对日期进行操作了, 无论是 昨天, 前天, 还是 三天前, 无论是 上周, 上个月, 前三个月, 半年前,都可以很快计算出日期.

    这样的话是不是很方便地,往后台传 我们想要的日期呢

      比如: 一个这样的需求

    我们需要获取这个日期段,精确到秒,

    废话不多说,下面就是js部分,自己可以测试一下,看看是不是符合自己的需求

     1       var timestamp=Math.round(new Date() / 1000)
     2 
     3       function timestampToTime(timestamp) {
     4 
     5         var date = new Date(timestamp * 1000);//时间戳为10位需*1000,时间戳为13位的话不需乘1000
     6         Y = date.getFullYear() + '-';
     7         M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
     8         D = date.getDate() < 10 ? '0'+ date.getDate() : date.getDate()+ ' ';
     9         h = date.getHours() < 10 ? '0'+date.getHours()+ ':' : date.getHours() + ':';
    10         m = date.getMinutes() < 10 ? '0'+date.getMinutes()+ ':' : date.getMinutes() + ':';
    11         s = date.getSeconds() < 10 ? '0'+date.getSeconds() : date.getSeconds();
    12         // D = date.getDate() + ' ';
    13         // h = date.getHours() + ':';
    14         // m = date.getMinutes() + ':';
    15         // s = date.getSeconds();
    16         return Y+M+D+h+m+s;
    17     }
    18     timestampToTime(timestamp*1000);
    19     console.log(timestamp)
    20     console.log(timestampToTime(timestamp));
  • 相关阅读:
    Windbg DUMP
    NET媒体文件操作组件TagLib
    NET Framework、.NET Core、Xamarin
    面向切面编程
    微服务
    NET Core
    Yeoman generator
    Service Fabric
    Vue.JS 2.x
    CoreCLR
  • 原文地址:https://www.cnblogs.com/gaoht/p/9238813.html
Copyright © 2020-2023  润新知