zoukankan      html  css  js  c++  java
  • js 时间戳格式化日期格式

    时间戳转换为日期,网上搜了好几个或多或少都有点问题,自己整理了一下,写了个方法

    console.log(formatDate(1565280000000))

    输出:

     2019-08-09 00:00:00

    Date.prototype.format =function(datetime)
    {
           var date = new Date(datetime);//时间戳为10位需*1000,时间戳为13位的话不需乘1000
                    var year = date.getFullYear(),
                        month = ("0" + (date.getMonth() + 1)).slice(-2),
                        sdate = ("0" + date.getDate()).slice(-2),
                        hour = ("0" + date.getHours()).slice(-2),
                        minute = ("0" + date.getMinutes()).slice(-2),
                        second = ("0" + date.getSeconds()).slice(-2);
                    // 拼接
                    var result = year + "-"+ month +"-"+ sdate +" "+ hour +":"+ minute +":" + second;
                    // 返回
                    return result;
    }
    formatDate(datetime) {
                    var date = new Date(datetime);//时间戳为10位需*1000,时间戳为13位的话不需乘1000
                    var year = date.getFullYear(),
                        month = ("0" + (date.getMonth() + 1)).slice(-2),
                        sdate = ("0" + date.getDate()).slice(-2),
                        hour = ("0" + date.getHours()).slice(-2),
                        minute = ("0" + date.getMinutes()).slice(-2),
                        second = ("0" + date.getSeconds()).slice(-2);
                    // 拼接
                    var result = year + "-"+ month +"-"+ sdate +" "+ hour +":"+ minute +":" + second;
                    // 返回
                    return result;
                }
  • 相关阅读:
    最优二叉查找树
    最长公共子序列问题
    最大子段和问题
    01背包问题
    浅析LRU(K-V)缓存
    LeetCode——LRU Cache
    LeetCode——Gas Station
    LeetCode——Jump Game II
    LeetCode——Jump Game
    LeetCode——Implement Trie (Prefix Tree)
  • 原文地址:https://www.cnblogs.com/langhua/p/11314815.html
Copyright © 2011-2022 走看看