thedesk/app/js/tl/date.js

99 lines
2.6 KiB
JavaScript
Raw Normal View History

2018-01-28 23:22:43 +11:00
//日付パーサー
2020-08-05 13:38:51 +10:00
function date(str, datetype, lastStatus) {
if (lastStatus) {
var date = new Date(str)
return date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + date.getDate()
} else if (datetype == "relative") {
2020-08-02 17:08:07 +10:00
return '<time class="timeago" datetime="' + str + '"></time>'
2018-01-28 23:22:43 +11:00
} else {
2020-08-02 17:08:07 +10:00
var date = new Date(str)
2018-01-28 23:22:43 +11:00
if (datetype == "unix") {
2020-08-02 17:08:07 +10:00
var unixm = date.getTime()
return Math.floor(unixm / 1000)
2018-01-28 23:22:43 +11:00
}
2020-08-02 17:08:07 +10:00
var now = new Date()
var month = date.getMonth() + 1
2018-01-28 23:22:43 +11:00
if (date.getMinutes() < 10) {
2020-08-02 17:08:07 +10:00
var min = "0" + date.getMinutes()
2018-01-28 23:22:43 +11:00
} else {
2020-08-02 17:08:07 +10:00
var min = date.getMinutes()
2018-01-28 23:22:43 +11:00
}
2020-08-02 17:08:07 +10:00
var sec = null
2018-01-28 23:22:43 +11:00
if (date.getSeconds() < 10) {
2020-08-02 17:08:07 +10:00
sec = "0" + date.getSeconds()
2018-01-28 23:22:43 +11:00
} else {
2020-08-02 17:08:07 +10:00
sec = date.getSeconds()
2018-01-28 23:22:43 +11:00
}
if (datetype == "full") {
2018-05-26 02:37:35 +10:00
var ret = date.getFullYear() + "/" + month + "/" + date.getDate() + "/ " +
2020-08-02 17:08:07 +10:00
date.getHours() + ":" + min + ":" + sec
2018-01-28 23:22:43 +11:00
}
if (date.getFullYear() == now.getFullYear()) {
if (date.getMonth() == now.getMonth()) {
if (date.getDate() == now.getDate()) {
if (datetype == "medium") {
2020-08-02 17:08:07 +10:00
var ret = '<time class="timeago" datetime="' + str + '"></time>'
2018-01-28 23:22:43 +11:00
} else {
2020-08-02 17:08:07 +10:00
var ret = date.getHours() + ":" + min + ":" + sec
2018-01-28 23:22:43 +11:00
}
} else {
2018-05-26 02:37:35 +10:00
var ret = month + "/" + date.getDate() + " " + date.getHours() + ":" +
2020-08-02 17:08:07 +10:00
min + ":" + sec
2018-01-28 23:22:43 +11:00
}
} else {
2018-05-26 02:37:35 +10:00
var ret = month + "/" + date.getDate() + " " + date.getHours() + ":" + min +
2020-08-02 17:08:07 +10:00
":" + sec
2018-01-28 23:22:43 +11:00
}
} else {
2018-05-26 02:37:35 +10:00
var ret = date.getFullYear() + "/" + month + "/" + date.getDate() + " " +
2020-08-02 17:08:07 +10:00
date.getHours() + ":" + min + ":" + sec
2018-01-28 23:22:43 +11:00
}
if (datetype == "double") {
2020-08-02 17:08:07 +10:00
return '<time class="timeago" datetime="' + str + '"></time>/' + ret
2018-01-28 23:22:43 +11:00
} else {
2020-08-02 17:08:07 +10:00
return ret
2018-01-28 23:22:43 +11:00
}
}
}
//特殊フォーマット(インスタンス情報で利用)
function crat(str) {
2020-08-02 17:08:07 +10:00
var date = new Date(str)
var mnt = null
2019-05-19 17:39:30 +10:00
if (date.getMonth() < 9) {
2020-08-02 17:08:07 +10:00
mnt = "0" + (date.getMonth() + 1)
2019-05-19 17:39:30 +10:00
} else {
2020-08-02 17:08:07 +10:00
mnt = date.getMonth() + 1
2019-02-01 03:30:25 +11:00
}
2019-05-19 17:39:30 +10:00
if (date.getDate() < 10) {
2020-08-02 17:08:07 +10:00
var dat = "0" + date.getDate()
2019-05-19 17:39:30 +10:00
} else {
2020-08-02 17:08:07 +10:00
var dat = date.getDate()
2019-02-01 03:30:25 +11:00
}
2019-05-19 17:39:30 +10:00
if (date.getHours() < 10) {
2020-08-02 17:08:07 +10:00
var hrs = "0" + date.getHours()
2019-05-19 17:39:30 +10:00
} else {
2020-08-02 17:08:07 +10:00
var hrs = date.getHours()
2019-02-01 03:30:25 +11:00
}
2019-05-19 17:39:30 +10:00
if (date.getMinutes() < 10) {
2020-08-02 17:08:07 +10:00
var mns = "0" + date.getMinutes()
2019-05-19 17:39:30 +10:00
} else {
2020-08-02 17:08:07 +10:00
var mns = date.getMinutes()
2019-02-01 03:30:25 +11:00
}
2019-05-19 17:39:30 +10:00
if (date.getSeconds() < 10) {
2020-08-02 17:08:07 +10:00
var sec = "0" + date.getSeconds()
2019-05-19 17:39:30 +10:00
} else {
2020-08-02 17:08:07 +10:00
var sec = date.getSeconds()
2019-02-01 03:30:25 +11:00
}
2020-08-02 17:08:07 +10:00
format_str = 'YYYY-MM-DD hh:mm:ss'
format_str = format_str.replace(/YYYY/g, date.getFullYear())
format_str = format_str.replace(/MM/g, mnt)
format_str = format_str.replace(/DD/g, dat)
format_str = format_str.replace(/hh/g, hrs)
format_str = format_str.replace(/mm/g, mns)
format_str = format_str.replace(/ss/g, sec)
2018-01-28 23:22:43 +11:00
2020-08-02 17:08:07 +10:00
return format_str
2018-01-28 23:22:43 +11:00
}