在开发中经常会用到时间格式化,有时候在网上搜索一大堆但不是自己想要的,自己总结一下,写一个时间格式化工具方便以后直接使用,欢迎大家来吐槽……

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
 * Created by linxins on 2016/6/16.
 */
if (typeof linxins !== 'function') {
    var linxins = function(){};
}
(function(){
    var _self = this.linxins;
    /**
     * 获取当前时间的格式化日期
     * @param string Fmt  eg:Y-m-d H:i:s
     * @param boolean hasZero  eg:true|false
     * @returns {string}
     */
    _self.dateFormat = function(Fmt, hasZero){
        return timeFormat(Fmt, hasZero);
    }
    /**
     * 将时间戳格式化
     * @param number timestamp  eg:1465963958000 length:13
     * @param string Fmt  eg:Y-m-d H:i:s
     * @param boolean hasZero  eg:true|false
     * @returns {string}
     */
    _self.timestampFormat =www.90168.org function(timestamp, Fmt, hasZero){
        return timeFormat(timestamp, Fmt, hasZero);
    }
    /**
     * 时间字符串转时间戳
     * @param string dateStr  eg:2016-06-16 16:15:59
     * @returns {number}
     */
    _self.dateStr2timestamp = function(dateStr){
        return (typeof dateStr === 'string') ? Date.parse(new Date(dateStr)) : Date.parse(new Date());
    }
    /**
     * 将时间戳格式化
     * @param number timestamp  eg:1465963958000 length:13
     * @param string Fmt  eg:Y-m-d H:i:s
     * @param boolean hasZero  eg:true|false
     * @returns {string}
     */
    function timeFormat(timestamp, Fmt, hasZero){
        var date = (typeof timestamp != 'undefined' && timestamp != '') ? new Date(timestamp) : new Date();
        var hasZero = (typeof hasZero === 'boolean') ? hasZero : true;
        var Y = date.getFullYear();
        var m = (hasZero && date.getMonth()+1 < 10) ? '0'+(date.getMonth()+1) : date.getMonth()+1;
        var d = (hasZero && date.getDate() < 10) ? '0'+date.getDate() : date.getDate();
        var H = (hasZero && date.getHours() < 10) ? '0'+date.getHours() : date.getHours();
        var i = (hasZero && date.getMinutes() < 10) ? '0'+date.getMinutes() : date.getMinutes();
        var s = (hasZero && date.getSeconds() < 10) ? '0'+date.getSeconds() : date.getSeconds();
        var fomateTime = '';
        switch (Fmt){
            case 'YmdHis':
                fomateTime = Y+m+d+H+i+s;
                break;
            case 'Y-m-d H:i:s':
                fomateTime = Y+'-'+m+'-'+d+' '+H+':'+i+':'+s;
                break;
            case 'Y/m/d H:i:s':
                fomateTime = Y+'/'+m+'/'+d+' '+H+':'+i+':'+s;
                break;
            case 'Y-m-d H:i':
                fomateTime = Y+'-'+m+'-'+d+' '+H+':'+i;
                break;
            case 'Y-m-d H':
                fomateTime = Y+'-'+m+'-'+d+' '+H;
                break;
            case 'Y-m-d':
                fomateTime = Y+'-'+m+'-'+d;
                break;
            case 'Ymd':
                fomateTime = Y + m + d;
                break;
            case 'H:i:s':
                fomateTime = H+':'+i+':'+s;
                break;
            default:
                fomateTime = Y+'-'+m+'-'+d+' '+H+':'+i+':'+s;
                break;
        }
        return fomateTime;
    }
})(window);

//测试datetimeUtil

console.log(linxins.dateFormat());//当前时间格式:2016-06-16 16:44:49

console.log(linxins.dateStr2timestamp('2016-06-15 12:12:38'));//1465963958000

console.log(linxins.timestampFormat(1465963958000, 'Y/m/d H:i:s', false));//

js时间格式化工具,时间戳格式化,字符串转时间戳的更多相关文章

  1. js获取时间和日期,字符串和时间戳之间的转换

    //获取当前时间: var myDate = new Date();//当前时间 var year = myDate.getFullYear();//当前年份 var month = myDate.g ...

  2. 调用get_str_time(时间), 就能把毫秒的时间转换成格式化的 ,转化时间戳的方法

    function get_str_time(time){ var datetime = new Date(); datetime.setTime(time); var year = datetime. ...

  3. JS时间格式转成字符串

    formatNumber = n => { n = n.toString(); return n[1] ? n : '0' + n }; // 时间格式化 formatTime = date = ...

  4. js 时间格式化工具类

    /** * 返回示例:0 天 4 小时 7 分钟 57 秒 * @param second 毫秒数 * @returns {String} 时间html */ function secondToDay ...

  5. MySQL 日期、字符串、时间戳互转

    背景 原文地址:https://www.cnblogs.com/jhy-ocean/p/5560857.html 平时比较常用的时间.字符串.时间戳之间的互相转换,虽然常用但是几乎每次使用时候都喜欢去 ...

  6. js 时间格式转换

    js时间格式转换 格式化时间转成时间戳 //格式化转时间戳(单位秒) function strtotime(strtime) { strtime = strtime.substring(0, 19); ...

  7. 时间戳显示为多少分钟前,多少天前的JS处理,JS时间格式化,时间戳的转换

    var dateDiff = function (timestamp) { // 补全为13位 var arrTimestamp = (timestamp + '').split(''); for ( ...

  8. js时间格式化函数,支持Unix时间戳

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  9. JS 时间字符串与时间戳之间的转换

    1.当前时间换时间戳 var timestamp = parseInt(new Date().getTime()/1000); // 当前时间戳 document.write(timestamp); ...

随机推荐

  1. java 通过文件后缀名查找文件

    最近开发项目的时候需要过滤出一些指定的文件,所以有了以下的一些代码: /** **该类主要是过滤得到指定后缀名的文件 **/ public class DataFileFilter implement ...

  2. ios 身份证照片识别信息

    一个近乎完整的可识别中国身份证信息的Demo就问问你霸气不

  3. 使用SharePreferences存取数据(慕课笔记 )

    0.视频地址:http://www.imooc.com/video/3265 1.使用SharePreferences存取数据: public class MainActivity extends A ...

  4. JFinal免费公开课更新中

    价值千元的课程,免费报名学习,JFinal学院-小木 录制JFinal视频教程,JFinal核心已经周边涉及到微信小程序开发.数据库.前端实战等.

  5. jsp四大作用域之page

    <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding= ...

  6. HDU 4055 The King’s Ups and Downs(DP计数)

    题意: 国王的士兵有n个,每个人的身高都不同,国王要将他们排列,必须一高一矮间隔进行,即其中的一个人必须同时高于(或低于)左边和右边.问可能的排列数.例子有1千个,但是最多只算到20个士兵,并且20个 ...

  7. 给VS2008 打补丁

    vs2003到2008各版本如下: vs.net2003 Visual Studio .NET 2003 Enterprise Architect Visual Studio .NET 2003 En ...

  8. Beta冲刺(周四)

    这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass1 这个作业要求在哪里 https://edu.cnblo ...

  9. 2018.3.4 Linux and Unix 知识点

    UNIX系统的特点 1.多任务 2.多用户 3.强大的网络功能 4.设备无关性 5.并行处理能力 6.开放性 7.错误处理 Linux系统的特点 1.自由软件 2.良好的兼容性 3.良好的界面 4.丰 ...

  10. 用promise封装ajax

    首先贴代码 var ajaxOptions = { url: 'url', method: 'GET', async: true, data: null, dataType: 'text', } fu ...