zoukankan      html  css  js  c++  java
  • js实现日期转换

    根据时间(2020-07-20 10:23:21)转换成秒,可用于两个时间的比较

    let time = new Date("2020-07-20 10:23:21").getTime();

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    获取时间毫秒 var time = Date.now();

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    字符串转换成日期格式:

    '20130505'.replace(/^(d{4})(d{2})(d{2})$/, "$1-$2-$3");   结果:  "2013-05-05"  ;

    toLocaleString获取的是本地时间(当前电脑的时间)

    new Date(+new Date()).toLocaleString()

    "2020/1/8 下午6:36:08"

    toISOString获取的是国际时间,和北京时间8小时时差。

    new Date(+new Date()+8*3600*1000).toISOString()

    "2020-01-08T10:36:02.157Z"

    方法1:

    function formate(){
                    var current = new Date(+new Date()+8*3600*1000).toISOString();
                    var day = current.split("T")[0];
                    var time = current.split("T")[1].split(".")[0];
                    return day+" "+time;
                }
                console.log(formate());

    方法2:别人总结的

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

    方法3:

    function getNowFormatDate() {
                    var date = new Date();
                    var seperator1 = "-";
                    var seperator2 = ":";
                    var month = date.getMonth() + 1;
                    var strDate = date.getDate();
                    if (month >= 1 && month <= 9) {
                        month = "0" + month;
                    }
                    if (strDate >= 0 && strDate <= 9) {
                        strDate = "0" + strDate;
                    }
                    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
                            + " " + date.getHours() + seperator2 + date.getMinutes()
                            + seperator2 + date.getSeconds();
                    return currentdate;
                }
                console.log(getNowFormatDate());

    原博地址:https://blog.csdn.net/weixin_30845171/article/details/96541586

  • 相关阅读:
    nodejs入门学习笔记一——一个完整的http路由服务实现
    安装centos虚拟机
    test
    sql单列合并
    linux系统的安全小知识
    建造者模式组装mybatis参数Example()
    Https网站搭建——通过https://localhost:8443访问tomcat首页
    妈妈再也不用担心我找不到spring源码了!
    Webpack --- 模块打包器
    CSS3弹性盒---怪异盒
  • 原文地址:https://www.cnblogs.com/changyuqing/p/12165324.html
Copyright © 2011-2022 走看看