您好,欢迎访问一九零五行业门户网

js时间戳转为日期格式的方法

什么是unix时间戳(unix timestamp): unix时间戳(unix timestamp),或称unix时间(unix time)、posix时间(posix time),是一种时间表示方式,定义为从格林威治时间1970年01月01日00时00分00秒起至现在的总秒数。unix时间戳不仅被使用在unix系统、类unix系统中,也在许多其他操作系统中被广泛采用。
目前相当一部分操作系统使用32位二进制数字表示时间。此类系统的unix时间戳最多可以使用到格林威治时间2038年01月19日03时14分07秒(二进制:01111111 11111111 11111111 11111111)。其后一秒,二进制数字会变为10000000 00000000 00000000 00000000,发生溢出错误,造成系统将时间误解为1901年12月13日20时45分52秒。这很可能会引起软件故障,甚至是系统瘫痪。使用64位二进制数字表示时间的系统(最多可以使用到格林威治时间292,277,026,596年12月04日15时30分08秒)则基本不会遇到这类溢出问题。
一.js将时间转换成时间戳
1.js获取当前时间戳的方法
var timestamp1 = date.parse(new date());var timestamp2 = (new date()).valueof();var timestamp3 = new date().gettime();
第一种:获取的时间戳是把毫秒改成000显示,第二种和第三种是获取了当前毫秒的时间戳。
2.js获取制定时间戳的方法
var oldtime = (new date("2015/06/23 08:00:20")).gettime()/1000;gettime()返回数值的单位是毫秒。
二.js把时间戳转为为普通日期格式
1.date tolocalestring方法
function getlocaltime(ns) { return new date(parseint(ns) * 1000).tolocalestring().replace(/:\d{1,2}$/,' '); }
parseint() 函数可解析一个字符串,并返回一个整数。
js中时间操作单位是毫秒。
tolocalestring() 方法可根据本地时间把 date 对象转换为字符串,并返回结果。
replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串。
replace(/:\d{1,2}$/,' ')验证替换以:开始有一位或二位数字的结束字符串,就是秒;替换为空
所以我们可以利用正则表达式改变我们想要的日期格式。
2.date 属性方法
function add0(m){return m<10?'0'+m:m }function format(shijianchuo){//shijianchuo是整数,否则要parseint转换var time = new date(shijianchuo);var y = time.getfullyear();var m = time.getmonth()+1;var d = time.getdate();var h = time.gethours();var mm = time.getminutes();var s = time.getseconds();return y+'-'+add0(m)+'-'+add0(d)+' '+add0(h)+':'+add0(mm)+':'+add0(s);}
三.封装的时间格式器
/** * 和php一样的时间戳格式化函数 * @param {string} format 格式 * @param {int} timestamp 要格式化的时间 默认为当前时间 * @return {string} 格式化的时间字符串 */function date(format, timestamp){ var a, jsdate=((timestamp) ? new date(timestamp*1000) : new date()); var pad = function(n, c){ if((n = n + "").length < c){ return new array(++c - n.length).join("0") + n; } else { return n; } }; var txt_weekdays = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"]; var txt_ordin = {1:"st", 2:"nd", 3:"rd", 21:"st", 22:"nd", 23:"rd", 31:"st"}; var txt_months = ["", "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"]; var f = { // day d: function(){return pad(f.j(), 2)}, d: function(){return f.l().substr(0,3)}, j: function(){return jsdate.getdate()}, l: function(){return txt_weekdays[f.w()]}, n: function(){return f.w() + 1}, s: function(){return txt_ordin[f.j()] ? txt_ordin[f.j()] : 'th'}, w: function(){return jsdate.getday()}, z: function(){return (jsdate - new date(jsdate.getfullyear() + "/1/1")) / 864e5 >> 0}, // week w: function(){ var a = f.z(), b = 364 + f.l() - a; var nd2, nd = (new date(jsdate.getfullyear() + "/1/1").getday() || 7) - 1; if(b <= 2 && ((jsdate.getday() || 7) - 1) <= 2 - b){ return 1; } else{ if(a = 4 && a >= (6 - nd)){ nd2 = new date(jsdate.getfullyear() - 1 + "/12/31"); return date("w", math.round(nd2.gettime()/1000)); } else{ return (1 + (nd > 0); } } }, // month f: function(){return txt_months[f.n()]}, m: function(){return pad(f.n(), 2)}, m: function(){return f.f().substr(0,3)}, n: function(){return jsdate.getmonth() + 1}, t: function(){ var n; if( (n = jsdate.getmonth() + 1) == 2 ){ return 28 + f.l(); } else{ if( n & 1 && n < 8 || !(n & 1) && n > 7 ){ return 31; } else{ return 30; } } }, // year l: function(){var y = f.y();return (!(y & 3) && (y % 1e2 || !(y % 4e2))) ? 1 : 0}, //o not supported yet y: function(){return jsdate.getfullyear()}, y: function(){return (jsdate.getfullyear() + "").slice(2)}, // time a: function(){return jsdate.gethours() > 11 ? "pm" : "am"}, a: function(){return f.a().touppercase()}, b: function(){ // peter paul koch: var off = (jsdate.gettimezoneoffset() + 60)*60; var theseconds = (jsdate.gethours() * 3600) + (jsdate.getminutes() * 60) + jsdate.getseconds() + off; var beat = math.floor(theseconds/86.4); if (beat > 1000) beat -= 1000; if (beat < 0) beat += 1000; if ((string(beat)).length == 1) beat = "00"+beat; if ((string(beat)).length == 2) beat = "0"+beat; return beat; }, g: function(){return jsdate.gethours() % 12 || 12}, g: function(){return jsdate.gethours()}, h: function(){return pad(f.g(), 2)}, h: function(){return pad(jsdate.gethours(), 2)}, i: function(){return pad(jsdate.getminutes(), 2)}, s: function(){return pad(jsdate.getseconds(), 2)}, //u not supported yet // timezone //e not supported yet //i not supported yet o: function(){ var t = pad(math.abs(jsdate.gettimezoneoffset()/60*100), 4); if (jsdate.gettimezoneoffset() > 0) t = "-" + t; else t = "+" + t; return t; }, p: function(){var o = f.o();return (o.substr(0, 3) + ":" + o.substr(3, 2))}, //t not supported yet //z not supported yet // full date/time c: function(){return f.y() + "-" + f.m() + "-" + f.d() + "t" + f.h() + ":" + f.i() + ":" + f.s() + f.p()}, //r not supported yet u: function(){return math.round(jsdate.gettime()/1000)} }; return format.replace(/[\]?([a-za-z])/g, function(t, s){ if( t!=s ){ // escaped ret = s; } else if( f[s] ){ // a date function exists ret = f[s](); } else{ // nothing special ret = s; } return ret; }); }
调用方法 能够很方便的将时间戳转换成日期的格式,如:
date('y-m-d','1350052653');//很方便的将时间戳转换成了2012-10-11 date('y-m-d h:i:s','1350052653');//得到的结果是2012-10-12 22:37:33
以上就是js时间戳转为日期格式的方法,希望对大家的学习有所帮助。
【相关教程推荐】
1. javascript视频教程
2. javascript在线手册
3. bootstrap教程
其它类似信息

推荐信息