• JS将时间与时间戳互转


    时间转为时间戳

    1
    var changeTimes = function(time) { 2 var temp = time.split(' '); 3 var arr = temp[0].split('-'); 4 var brr = temp[1].split(':'); 5 if(brr.length == 3){ 6 var timestamp = new Date(Date.UTC(arr[0], arr[1] - 1, arr[2], +brr[0] - 8, brr[1], brr[2])); 7 }else if(brr.length == 2){ 8 var timestamp = new Date(Date.UTC(arr[0], arr[1] - 1, arr[2], +brr[0] - 8, brr[1])); 9 } 10 var timestamp = timestamp.getTime() / 1000; 11 return timestamp; 12 };

    时间戳转为时间

    通过js将时间戳转换成"yyyy--mm--dd"格式
    
    function fmtDate(timestamp){
        return formatDate(timestamp)
    }
    function formatDate(timestamp){
      var date = new Date(timestamp)
      var o = {       
            "y" : date.getFullYear(),         
            "M" : date.getMonth()+1,      
            "d" : date.getDate(),
            "h" : date.getHours(),
            "m" : date.getMinutes(),
            "s" : date.getSeconds(),
        }
        o.M = (o.M<10)?('0'+o.M):o.M;
        o.d = (o.d<10)?('0'+o.d):o.d;
        o.h = (o.h<10)?('0'+o.h):o.h;
        o.m = (o.m<10)?('0'+o.m):o.m;
        o.s = (o.s<10)?('0'+o.s):o.s;    
        return `${o.y}-${o.M}-${o.d} ${o.h}:${o.m}:${o.s}`
    }

    时间格式化

    1

    function formatDate(val) { 
        var o = {       
            "y" : val.getFullYear(),         
            "M" : val.getMonth()+1,      
            "d" : val.getDate(),
            "h" : val.getHours(),
            "m" : val.getMinutes(),
            "s" : val.getSeconds(),
        }
        o.M = (o.M<10)?('0'+o.M):o.M;
        o.d = (o.d<10)?('0'+o.d):o.d;
        o.h = (o.h<10)?('0'+o.h):o.h;
        o.m = (o.m<10)?('0'+o.m):o.m;
        o.s = (o.s<10)?('0'+o.s):o.s;    
        return `${o.y}-${o.M}-${o.d} ${o.h}:${o.m}:${o.s}`
      }

    2

    Date.prototype.format = function(format) {
           var date = {
                  "M+": this.getMonth() + 1,
                  "d+": this.getDate(),
                  "h+": this.getHours(),
                  "m+": this.getMinutes(),
                  "s+": this.getSeconds(),
                  "q+": Math.floor((this.getMonth() + 3) / 3),
                  "S+": this.getMilliseconds()
           };
           if (/(y+)/i.test(format)) {
                  format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
           }
           for (var k in date) {
                  if (new RegExp("(" + k + ")").test(format)) {
                         format = format.replace(RegExp.$1, RegExp.$1.length == 1
                                ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
                  }
           }
           return format;
    }
    console.log(newDate.format('yyyy-MM-dd h:m:s'));

    Date.UTC 函数 (JavaScript)

    返回协调通用时间 (UTC)(或 GMT)1970 年 1 月 1 日午夜与所指定的日期之间相差的毫秒数。

    语法:

    Date.UTC(year, month, day[, hours[, minutes[, seconds[,ms]]]]) 

    Date.UTC 函数返回从 UTC 1970 年 1 月 1 日午夜到所提供日期之间的毫秒数。  此返回值可用在 setTime 方法和 Date 对象构造函数中。  如果参数值大于其范围或为负数,则其他存储的值都将得到相应的修改。  例如,如果指定 150 秒,则 JavaScript 将该数字重新定义为 2 分 30 秒。  

    Date.UTC 函数和接受日期的 Date 对象构造函数之间的差别在于:Date.UTC 函数采用 UTC,而 Date 对象构造函数采用当地时间。

     一、计算当前时间后一个月的日期

    function getMonthAfter(){
       let timestamp = Date.parse(new Date()) + 30*24*3600*1000;
       let newDate = new Date(timestamp);
       return newDate.format('yyyy-MM-dd'); //用到二里的format方法
    }

    二、将时间戳转为日期

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript">
    // 获取当前时间戳(以s为单位)
    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;
    //当前时间戳为:1403149534
    console.log("当前时间戳为:" + timestamp);
    

    // 获取某个时间格式的时间戳 var stringTime = "2014-07-10 10:21:12"; var timestamp2 = Date.parse(new Date(stringTime)); timestamp2 = timestamp2 / 1000; //2014-07-10 10:21:12的时间戳为:1404958872 console.log(stringTime + "的时间戳为:" + timestamp2); // 将当前时间换成时间格式字符串 var timestamp3 = 1403058804; var newDate = new Date(); newDate.setTime(timestamp3 * 1000); // Wed Jun 18 2014 console.log(newDate.toDateString()); // Wed, 18 Jun 2014 02:33:24 GMT console.log(newDate.toGMTString()); // 2014-06-18T02:33:24.000Z console.log(newDate.toISOString()); // 2014-06-18T02:33:24.000Z console.log(newDate.toJSON()); // 2014年6月18日 console.log(newDate.toLocaleDateString()); // 2014年6月18日 上午10:33:24 console.log(newDate.toLocaleString()); // 上午10:33:24 console.log(newDate.toLocaleTimeString()); // Wed Jun 18 2014 10:33:24 GMT+0800 (中国标准时间) console.log(newDate.toString()); // 10:33:24 GMT+0800 (中国标准时间) console.log(newDate.toTimeString()); // Wed, 18 Jun 2014 02:33:24 GMT console.log(newDate.toUTCString()); </script>

      

  • 相关阅读:
    【原创翻译】The Free Lunch Is Over
    【原创】基于ZYNQ7000的交叉编译工具链Qt+OpenCV+ffmpeg等库支持总结(二)
    【原创】基于ZYNQ7000的交叉编译工具链Qt+OpenCV+ffmpeg等库支持总结(一)
    实现内容提供者步骤
    为什么需要内容提供者
    aidl的应用场景
    aidl介绍
    百度音乐盒框架
    通过接口方式调用服务里面的方法
    通过bindservice方式调用服务方法里面的过程
  • 原文地址:https://www.cnblogs.com/jessiespur/p/6824118.html
Copyright © 2020-2023  润新知