Created
May 31, 2016 10:00
-
-
Save iahu/2b2b9b5316a0d50e071f83ff91d6077b to your computer and use it in GitHub Desktop.
最近更新时间
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function zPad (n) { | |
var l = 2; | |
var z = new Array(l+1).join('0'); | |
return (z+n).slice(-l); | |
} | |
function autoTimeString(s) { | |
s = '' + s.replace(/^\s+/,'').replace(/\s+$/, ''); | |
if ( s.length === 10 ) { | |
s = s + '000'; | |
} | |
s = +s; | |
var d = new Date(s); | |
var now = new Date(); | |
var Y = d.getFullYear(); | |
var M = zPad(d.getMonth() + 1); | |
var D = zPad(d.getDate()); | |
var HH = zPad(d.getHours()); | |
var MM = zPad(d.getMinutes()); | |
var hours, secondes; | |
secondes = (now - d) / 1000; | |
hours = Math.ceil(secondes / (60*60) ); | |
if ( hours < 1 ) { | |
return Math.ceil(secondes/60) + ' 分钟前'; | |
} | |
if ( hours < 24 ) { | |
return hours + ' 小时前'; | |
} | |
if ( hours > 24 && hours < 48 ) { | |
return '昨天 ' + [HH, MM].join(':'); | |
} | |
// 跨年 | |
if ( d.getFullYear() !== now.getFullYear() ) { | |
return [Y, M, D].join('-') + ' ' + [HH, MM].join(':'); | |
} | |
return [M, D].join('-') + ' ' + [HH, MM].join(':'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment