• js获取当地时间并且拼接时间格式的三种方式


        js获取当地时间并且拼接时间格式,在stackoverflow上有人在问,查了资料,各种方法将时间格式改成任意自己想要的样式。

        1.

    [javascript] view plain copy
    print?
    1. var date = new Date(+new Date()+8*3600*1000).toISOString().replace(/T/g,' ').replace(/.[d]{3}Z/,'');  
    2. console.log(date);//2017-01-22 11:08:46  
        2.        
    [javascript] view plain copy
    print?
    1. var date = new Date();  
    2. var strDate = date.toLocaleString().replace(/[年月]/g,'-').replace(/[日上下午]/g,'');  
    3. console.log(strDate);//  2017/1/22 11:11:56  
       3.字符串拼接方式
    [javascript] view plain copy
    print?
    1.   function getNowFormatDate() {  
    2.             var date = new Date();  
    3.     var seperator1 = "-";  
    4.     var seperator2 = ":";  
    5.     var month = date.getMonth() + 1;  
    6.      
    7.     var strDate = date.getDate();  
    8.     if (month >= 1 && month <= 9) {  
    9.         month = "0" + month;  
    10.     }  
    11.     if (strDate >= 0 && strDate <= 9) {  
    12.         strDate = "0" + strDate;  
    13.     }  
    14.       
    15.     var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate  
    16.             + " " + date.getHours() + seperator2 + date.getMinutes()  
    17.             + seperator2 + date.getSeconds();  
    18.     return currentdate;  
    19. }   
    20. var data = getNowFormatDate();  
    21. console.log(data);  //2017-01-22 11:14:16  
  • 相关阅读:
    apply 和 call 方法详解【转载】
    npm无法安装node-sass的解决方法
    rem
    创建和存储 cookie
    webpack学习(一):webpack的安装和命令行
    gulp常用的插件
    移动前端开发之viewport的深入理解
    JavaSE中Collection集合框架学习笔记(3)——遍历对象的Iterator和收集对象后的排序
    JavaSE中Collection集合框架学习笔记(2)——拒绝重复内容的Set和支持队列操作的Queue
    JavaSE中Collection集合框架学习笔记(1)——具有索引的List
  • 原文地址:https://www.cnblogs.com/jpfss/p/9181580.html
Copyright © 2020-2023  润新知