zoukankan      html  css  js  c++  java
  • js快速获取当前时间并且返回想要的格式

    function backCurrentTime (type) {
        let currentTime=new Date( new Date() + 8 * 3600 * 1000 ).toJSON().substr(0,19).replace("T"," ");
        if(type=='y'){
            return currentTime.substr(0,4)
        }else if(type=='m'){
            return currentTime.substr(5,2)
        }else if(type=='d'){
            return currentTime.substr(8,2)
        }else if(type=='y-m'){
            return currentTime.substr(0,7)
        }else if(type=='y-m-d'){
            return currentTime.substr(0,10)
        }else if(type=='y-m-d-h-m'){
            return currentTime.substr(0,16)
        }else{
            return currentTime
        }
    }
    console.log('当前年',backCurrentTime('y') )
    console.log('当前月',backCurrentTime('m') )
    console.log('当前天',backCurrentTime('d') )
    console.log('当前年-月-天',backCurrentTime('y-m-d') )
    console.log('当前年-月-天-时-分',backCurrentTime('y-m-d-h-m') )
    
    主要是利用toJSON转格林威治时间 在+8小时得到就是北京时间
    参考地址:https://www.cnblogs.com/youwei716/p/14088544.html
    

    作者:明月人倚楼
    出处:https://www.cnblogs.com/IwishIcould/

    想问问题,打赏了卑微的博主,求求你备注一下的扣扣或者微信;这样我好联系你;(っ•̀ω•́)っ✎⁾⁾!

    如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,或者关注博主,在此感谢!

    万水千山总是情,打赏5毛买辣条行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主(っ•̀ω•́)っ✎⁾⁾!

    想问问题,打赏了卑微的博主,求求你备注一下的扣扣或者微信;这样我好联系你;(っ•̀ω•́)っ✎⁾⁾!

    支付宝
    微信
    本文版权归作者所有,欢迎转载,未经作者同意须保留此段声明,在文章页面明显位置给出原文连接
    如果文中有什么错误,欢迎指出。以免更多的人被误导。
  • 相关阅读:
    mysql多行多列合并为一行一列
    http://testyourvocab.com/
    数据导入报错:Got a packet bigger than‘max_allowed_packet’bytes的问题
    mybatis mapper xml 使用 > <
    Mysql limit
    table tr,td,div ... focus
    Java开发必备的9个英文网站
    MyEclipse中导入Spring 4.0源码
    《算法竞赛入门经典》1.5.6上机练习
    《算法竞赛入门经典》第一章1.5
  • 原文地址:https://www.cnblogs.com/IwishIcould/p/14771277.html
Copyright © 2011-2022 走看看