js时间格式化工具,时间戳格式化,字符串转时间戳

天师符      2022-02-06     670

关键词:

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

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时间戳转日期时间(代码片段)

JS日期格式化时间戳转日期时间字符串时间格式化functiontimeFomat(t,e)vars=t?newDate(t):newDate(1e3*t),i=s.getFullYear(),n=s.getMonth()+1,a=s.getDate(),o=s.getHours(),l=s.getMinutes(),u=s.getSe 查看详情

js时间戳怎么转成日期格式

...eString().replace(/:\d1,2$/,'');parseInt()函数可解析一个字符串,并返回一个整数。js中时间操作单位是毫秒。toLocaleString()方法可根据本地时间把Date对象转换为字符串,并返回结果。replace()方法用于在字符串中用一些字符替换... 查看详情

js时间格式处理(时间戳,时间格式,时间转换等)

一、时间转时间戳console.log(Date.parse(newDate()))//结果:1477808630000不推荐这种办法,毫秒级别的数值被转化为000console.log(newDate()).valueOf())//结果:1477808630404通过valueOf()函数返回指定对象的原始值获得准确的时间戳值console.log(newDate().g... 查看详情

js时间戳怎么转成日期格式

参考技术A使用Date对象可以将毫秒时间戳转为js的Date对象<br>然后再调用Date的getFullYear、getMonth、getDate等方法拼成想要的日期格式<br>vardate=newDate(1433665089755);<br>alert(date.getFullYear()+'/'+(date.getMonth()+1)+'/'+date.g... 查看详情

js中怎么样时间格式转成时间戳

参考技术A将日期格式转换成时间戳://也很简单date=newDate('2014-04-2318:55:49:123');//传入一个时间格式,如果不传入就是获取现在的时间了,就这么简单。//有三种方式获取time1=date.getTime()time2=date.valueOf()time3=Date.parse(date)//三种... 查看详情

php日期转时间戳

UNIX时间戳和格式化日期是我们常打交道的两个时间表示形式,Unix时间戳存储、处理方便,但是不直观,格式化日期直观,但是处理起来不如Unix时间戳那么自如,所以有的时候需要互相转换,下面给出PHP日期转时间戳、MySQL日期... 查看详情

js时间戳怎么转成日期格式

...-07-09 19:44:01更多自定义参数和用法可以参照:javascript格式化日期时间下面是源码的截图:js时间戳转为日期格式参考技术A时间戳就是从1970/1/10:00:00起到现在的秒数(按格林威治时间算),根据这个就能转换啦。最简单的方法... 查看详情

js时间戳格式化日期时间由于mysql数据库里面存储时间存的是时间戳,取出来之后,js要格式化一下显示。

//时间戳转时间functionRiQi(sj){varnow=newDate(sj*1000);varyear=now.getFullYear();varmonth=now.getMonth()+1;vardate=now.getDate();varhour=now.getHours();varminute=now.getMinutes();varsecond=now.getSeconds(); 查看详情

js内置对象-date-时间戳

...t;当前时间=====构造函数传日期字符串指定具体的日期 2、格式化时间:data.tostring()默认的时间格式,让日期以标准化的日期字符串格式输出 data.toLocaleString()当地的时间格式本地化日期字符串格式输出 3、getFullYear();获取年 get 查看详情

sqlserver时间戳转日期格式(13位时间戳)

参考技术AselectCONVERT(varchar(100),DATEADD(S,(出生日期时间戳+28800000)/1000,'1970-01-0100:00:00'),23) 查看详情

时间戳转为日期格式

参考技术A在线时间戳转换工具在java中获取时间戳方式:下面方法中,在调用Date构造函数而不传递参数的情况下,新创建的对象自动获得当前日期和时间。如果想传入特定日期,需将表示日期的字符串传递给Date构造函数。 查看详情

js格式化时间戳

//js格式化时间戳,转换为时间格式 2017-1-154:10:15functiongetLocalTime(nS){vartime=newDate(parseInt(nS)*1000).toLocaleString();varreg=newRegExp("(/)","g");varreg_time=newRegExp("(上午|下午)","g");returntime.replace 查看详情

jquery时间戳怎么转成日期格式

...  myTime:             /**             * 当前时间戳             * @return <int>        unix时间戳(秒)               */            CurTime: function()                return Date.parse(new D... 查看详情

js时间戳怎么转成日期格式

可以使用newDate()将时间戳转换成Date对象:var dt = new Date(1498282171331);//时间戳为参数然后就可以使用Date对象的一些方法来取得需要的部分了:var y = dt.getFullYear();var m = dt.getMonth()+1;var d =&nbs... 查看详情

mongodb时间戳转日期格式统计(代码片段)

在日常中经常出现时间戳存在mongodb里的情况,以下就是根据时间戳转日期格式生成数据db.test.insert("id":20162,"create_time":1554195600,"time":"2019-04-0217:00:00");db.test.insert("id":20159,"create_time":1552635000,"time":"2019-03-1515:30 查看详情

time模块

...在Python中,通常有这几种方式来表示时间:1)时间戳2)格式化的时间字符串3)元组(struct_time)共九个元素。datetime用于时间的加减time三种形式,时间戳、格式化,元祖#时间戳(timestamp)——从1970到现在有多少秒time.time()#格... 查看详情

js时间戳转换时间格式

functiongetLocalTime(time){ if(time>0){ vardateStr=newDate(time*1000); varstr=""+dateStr.getFullYear()+"-"; varm=dateStr.getMonth(); m=m<10?"0"+m:m; str+=m+"-"; vard=dateStr.getDate(); d 查看详情

js怎么把时间戳转换为日期格式

...期控件中拿到日期,然后参与计算,下边记录一个把日期字符串转换成时间戳的小函数。dateStr格式为“2014-05-0800:22:11”functionget_unix_time(dateStr)varnewstr=dateStr.replace(/-/g,'/');vardate=newDate(newstr);vartime_str=date.getTime().toString();returnt... 查看详情