Last active
January 4, 2016 05:51
-
-
Save giautm/b9f61bdc3aae150f6fa0 to your computer and use it in GitHub Desktop.
Định dạng thời gian
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
var dateTimeFormat = { | |
format: function (seconds) { | |
var now = new Date(); | |
var secFromNow = Math.floor(now.getTime() / 1000) - seconds; | |
var date = new Date(seconds * 1000); | |
var hour = date.getHours(); | |
var minute = date.getMinutes(); | |
if (secFromNow < 60) { // Dưới một phút | |
return (secFromNow < 0 ? 0 : secFromNow) + ' giây trước' | |
} else if (secFromNow < 3600) { // Dưới 1 giờ | |
return Math.floor(secFromNow / 60) + ' phút trước' | |
} else if (secFromNow < 43200) { // Dưới 1 ngày | |
return Math.floor(secFromNow / 3600) + ' tiếng trước' | |
} else if (secFromNow < 518400) { // Dưới 1 tuần | |
var c = undefined; | |
var days = (now.getDay() + 7 - date.getDay()) % 7; | |
if (days == 0) { | |
c = 'hôm nay' | |
} else if (days == 1) { | |
c = 'hôm qua' | |
} else { | |
c = ['Chủ Nhật', 'Thứ Hai', 'Thứ Ba', 'Thứ Tư', 'Thứ Năm', 'Thứ Sáu', 'Thứ Bảy'][date.getDay()] | |
} | |
var partOfDay = [ | |
{hour: 11, name: 'sáng'}, | |
{hour: 14, name: 'trưa'}, | |
{hour: 19, name: 'chiều'}, | |
{hour: 24, name: 'tối'} | |
].find(function (p) { | |
return hour < p.hour; | |
}).name; | |
return (hour % 12) + ':' + this.padZeroLeft(minute) + ' ' + partOfDay + ' ' + c; | |
} | |
return this.padZeroLeft(hour) + ':' + this.padZeroLeft(minute) + ' ' + | |
this.padZeroLeft(date.getDate()) + '/' + | |
this.padZeroLeft(date.getMonth() + 1) + '/' + date.getFullYear(); | |
}, | |
padZeroLeft: function (a) { | |
return a < 10 ? ('0' + a) : a | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment