zoukankan      html  css  js  c++  java
  • 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  
  • 相关阅读:
    MOS管基本构造和工作原理
    压控恒流源电路
    TI博客文章-4-20mA电流环路发送器入门
    node.js发http请求
    node.js之web开发 koa入门
    nodejs入门开发与常用模块
    node.js安装与入门使用
    node.js和前端js有什么区别
    php hash_hmac 与python hmac 区别
    redis命令使用
  • 原文地址:https://www.cnblogs.com/jpfss/p/9181580.html
Copyright © 2011-2022 走看看