|
|
|
@ -1,16 +1,14 @@
|
|
|
|
|
/** Constants used for time computations */ |
|
|
|
|
BI.Date = BI.Date || {}; |
|
|
|
|
BI.Date.SECOND = 1000; |
|
|
|
|
BI.Date.MINUTE = 60 * BI.Date.SECOND; |
|
|
|
|
BI.Date.HOUR = 60 * BI.Date.MINUTE; |
|
|
|
|
BI.Date.DAY = 24 * BI.Date.HOUR; |
|
|
|
|
BI.Date.WEEK = 7 * BI.Date.DAY; |
|
|
|
|
import {getDate, getTime} from "../2.base"; |
|
|
|
|
|
|
|
|
|
// Monday first, etc.
|
|
|
|
|
BI.Date._FD = 1; |
|
|
|
|
|
|
|
|
|
// short month names
|
|
|
|
|
BI.Date._SMN = [0, |
|
|
|
|
const SECOND = 1000; |
|
|
|
|
const MINUTE = 60 * SECOND; |
|
|
|
|
const HOUR = 60 * MINUTE; |
|
|
|
|
const DAY = 24 * HOUR; |
|
|
|
|
const WEEK = 7 * DAY; |
|
|
|
|
const _FD = 1; |
|
|
|
|
const _SMN = [ |
|
|
|
|
0, |
|
|
|
|
1, |
|
|
|
|
2, |
|
|
|
|
3, |
|
|
|
@ -21,55 +19,78 @@ BI.Date._SMN = [0,
|
|
|
|
|
8, |
|
|
|
|
9, |
|
|
|
|
10, |
|
|
|
|
11]; |
|
|
|
|
|
|
|
|
|
/** Adds the number of days array to the Date object. */ |
|
|
|
|
BI.Date._MD = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; |
|
|
|
|
11 |
|
|
|
|
]; |
|
|
|
|
const _MD = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; |
|
|
|
|
const _OFFSET = [0, -1, -2, -3, -4, -5, -6]; // 实际上无论周几作为一周的第一天,周初周末都是在-6-0间做偏移,用一个数组就可以
|
|
|
|
|
|
|
|
|
|
// 实际上无论周几作为一周的第一天,周初周末都是在-6-0间做偏移,用一个数组就可以
|
|
|
|
|
BI.Date._OFFSET = [0, -1, -2, -3, -4, -5, -6]; |
|
|
|
|
export const Date = { |
|
|
|
|
SECOND, |
|
|
|
|
MINUTE, |
|
|
|
|
HOUR, |
|
|
|
|
DAY, |
|
|
|
|
WEEK, |
|
|
|
|
_FD, |
|
|
|
|
_SMN, |
|
|
|
|
_MD, |
|
|
|
|
_OFFSET, |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
BI._.extend(BI, { |
|
|
|
|
/** |
|
|
|
|
* 获取时区 |
|
|
|
|
* @returns {String} |
|
|
|
|
*/ |
|
|
|
|
getTimezone: function (date) { |
|
|
|
|
export function getTimezone(date) { |
|
|
|
|
return date.toString().replace(/^.* (?:\((.*)\)|([A-Z]{1,4})(?:[\-+][0-9]{4})?(?: -?\d+)?)$/, "$1$2").replace(/[^A-Z]/g, ""); |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** Returns the number of days in the current month */ |
|
|
|
|
getMonthDays: function (date, month) { |
|
|
|
|
/** |
|
|
|
|
* Returns the number of days in the current month |
|
|
|
|
*/ |
|
|
|
|
export function getMonthDays(date, month = date.getMonth()) { |
|
|
|
|
var year = date.getFullYear(); |
|
|
|
|
if (typeof month === "undefined") { |
|
|
|
|
month = date.getMonth(); |
|
|
|
|
} |
|
|
|
|
if (((0 == (year % 4)) && ((0 != (year % 100)) || (0 == (year % 400)))) && month == 1) { |
|
|
|
|
if (((0 === (year % 4)) && ((0 !== (year % 100)) || (0 === (year % 400)))) && month === 1) { |
|
|
|
|
return 29; |
|
|
|
|
} |
|
|
|
|
return BI.Date._MD[month]; |
|
|
|
|
return _MD[month]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
}, |
|
|
|
|
/** |
|
|
|
|
* 获取每月的最后一天 |
|
|
|
|
* @returns {Date} |
|
|
|
|
*/ |
|
|
|
|
export function getLastDateOfMont(date) { |
|
|
|
|
return getDate(date.getFullYear(), date.getMonth(), getMonthDays(date)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 获取每月的最后一天 |
|
|
|
|
* @returns {Date} |
|
|
|
|
*/ |
|
|
|
|
getLastDateOfMonth: function (date) { |
|
|
|
|
return BI.getDate(date.getFullYear(), date.getMonth(), BI.getMonthDays(date)); |
|
|
|
|
}, |
|
|
|
|
export function getLastDateOfMonth(date) { |
|
|
|
|
return getDate(date.getFullYear(), date.getMonth(), getMonthDays(date)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** Returns the number of day in the year. */ |
|
|
|
|
getDayOfYear: function (date) { |
|
|
|
|
var now = BI.getDate(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0); |
|
|
|
|
var then = BI.getDate(date.getFullYear(), 0, 0, 0, 0, 0); |
|
|
|
|
/** |
|
|
|
|
* Returns the number of day in the year. |
|
|
|
|
* @param date |
|
|
|
|
* @returns {number} |
|
|
|
|
*/ |
|
|
|
|
export function getDayOfYear(date) { |
|
|
|
|
var now = getDate(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0); |
|
|
|
|
var then = getDate(date.getFullYear(), 0, 0, 0, 0, 0); |
|
|
|
|
var time = now - then; |
|
|
|
|
return Math.floor(time / BI.Date.DAY); |
|
|
|
|
}, |
|
|
|
|
return Math.floor(time / DAY); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** Returns the number of the week in year, as defined in ISO 8601. */ |
|
|
|
|
getWeekNumber: function (date) { |
|
|
|
|
var d = BI.getDate(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0); |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Returns the number of the week in year, as defined in ISO 8601. |
|
|
|
|
* @param date |
|
|
|
|
* @returns {number} |
|
|
|
|
*/ |
|
|
|
|
export function getWeekNumber(date) { |
|
|
|
|
var d = getDate(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0); |
|
|
|
|
var week = d.getDay(); |
|
|
|
|
var startOfWeek = BI.StartOfWeek % 7; |
|
|
|
|
var middleDay = (startOfWeek + 3) % 7; |
|
|
|
@ -82,31 +103,40 @@ BI._.extend(BI, {
|
|
|
|
|
d.setMonth(0); |
|
|
|
|
d.setDate(1); |
|
|
|
|
return Math.floor((ms - d.valueOf()) / (7 * 864e5)) + 1; |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
getQuarter: function (date) { |
|
|
|
|
export function getQuarter(date) { |
|
|
|
|
return Math.floor(date.getMonth() / 3) + 1; |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 离当前时间多少天的时间
|
|
|
|
|
getOffsetDate: function (date, offset) { |
|
|
|
|
return BI.getDate(BI.getTime(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds()) + offset * 864e5); |
|
|
|
|
}, |
|
|
|
|
/** |
|
|
|
|
* 离当前时间多少天的时间 |
|
|
|
|
* @param date |
|
|
|
|
* @param offset |
|
|
|
|
* @returns {Date} |
|
|
|
|
*/ |
|
|
|
|
export function getOffsetDate(date, offset) { |
|
|
|
|
return getDate(getTime(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds()) + offset * 864e5); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
getOffsetQuarter: function (date, n) { |
|
|
|
|
var dt = BI.getDate(BI.getTime(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds())); |
|
|
|
|
export function getOffsetQuarter(date, n) { |
|
|
|
|
var dt = getDate(getTime(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds())); |
|
|
|
|
var day = dt.getDate(); |
|
|
|
|
var monthDay = BI.getMonthDays(BI.getDate(dt.getFullYear(), dt.getMonth() + BI.parseInt(n) * 3, 1)); |
|
|
|
|
var monthDay = BI.getMonthDays(getDate(dt.getFullYear(), dt.getMonth() + parseInt(n, 10) * 3, 1)); |
|
|
|
|
if (day > monthDay) { |
|
|
|
|
day = monthDay; |
|
|
|
|
} |
|
|
|
|
dt.setDate(day); |
|
|
|
|
dt.setMonth(dt.getMonth() + parseInt(n) * 3); |
|
|
|
|
dt.setMonth(dt.getMonth() + parseInt(n, 10) * 3); |
|
|
|
|
return dt; |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 得到本季度的起始月份
|
|
|
|
|
getQuarterStartMonth: function (date) { |
|
|
|
|
/** |
|
|
|
|
* 得到本季度的起始月份 |
|
|
|
|
* @param date |
|
|
|
|
* @returns {number} |
|
|
|
|
*/ |
|
|
|
|
export function getQuarterStartMonth(date) { |
|
|
|
|
var quarterStartMonth = 0; |
|
|
|
|
var nowMonth = date.getMonth(); |
|
|
|
|
if (nowMonth < 3) { |
|
|
|
@ -122,67 +152,96 @@ BI._.extend(BI, {
|
|
|
|
|
quarterStartMonth = 9; |
|
|
|
|
} |
|
|
|
|
return quarterStartMonth; |
|
|
|
|
}, |
|
|
|
|
// 获得本季度的起始日期
|
|
|
|
|
getQuarterStartDate: function (date) { |
|
|
|
|
return BI.getDate(date.getFullYear(), BI.getQuarterStartMonth(date), 1); |
|
|
|
|
}, |
|
|
|
|
// 得到本季度的结束日期
|
|
|
|
|
getQuarterEndDate: function (date) { |
|
|
|
|
var quarterEndMonth = BI.getQuarterStartMonth(date) + 2; |
|
|
|
|
return BI.getDate(date.getFullYear(), quarterEndMonth, BI.getMonthDays(date, quarterEndMonth)); |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 指定日期n个月之前或之后的日期
|
|
|
|
|
getOffsetMonth: function (date, n) { |
|
|
|
|
var dt = BI.getDate(BI.getTime(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds())); |
|
|
|
|
/** |
|
|
|
|
* 获得本季度的起始日期 |
|
|
|
|
* @param date |
|
|
|
|
* @returns {Date} |
|
|
|
|
*/ |
|
|
|
|
export function getQuarterStartDate(date) { |
|
|
|
|
return getDate(date.getFullYear(), getQuarterStartMonth(date), 1); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 得到本季度的结束日期 |
|
|
|
|
* @param date |
|
|
|
|
* @returns {Date} |
|
|
|
|
*/ |
|
|
|
|
export function getQuarterEndDate(date) { |
|
|
|
|
var quarterEndMonth = getQuarterStartMonth(date) + 2; |
|
|
|
|
return getDate(date.getFullYear(), quarterEndMonth, getMonthDays(date)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 指定日期n个月之前或之后的日期 |
|
|
|
|
* @param date |
|
|
|
|
* @param n |
|
|
|
|
* @returns {Date} |
|
|
|
|
*/ |
|
|
|
|
export function getOffsetMonth(date, n) { |
|
|
|
|
var dt = getDate(getTime(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds())); |
|
|
|
|
var day = dt.getDate(); |
|
|
|
|
var monthDay = BI.getMonthDays(BI.getDate(dt.getFullYear(), dt.getMonth() + parseInt(n), 1)); |
|
|
|
|
var monthDay = getMonthDays(getDate(dt.getFullYear(), dt.getMonth() + parseInt(n, 10), 1)); |
|
|
|
|
if (day > monthDay) { |
|
|
|
|
day = monthDay; |
|
|
|
|
} |
|
|
|
|
dt.setDate(day); |
|
|
|
|
dt.setMonth(dt.getMonth() + parseInt(n)); |
|
|
|
|
dt.setMonth(dt.getMonth() + parseInt(n, 10)); |
|
|
|
|
return dt; |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 获得本周的起始日期
|
|
|
|
|
getWeekStartDate: function (date) { |
|
|
|
|
/** |
|
|
|
|
* 获得本周的起始日期 |
|
|
|
|
* @param date |
|
|
|
|
* @returns {Date} |
|
|
|
|
*/ |
|
|
|
|
export function getWeekStartDate(date) { |
|
|
|
|
var w = date.getDay(); |
|
|
|
|
var startOfWeek = BI.StartOfWeek % 7; |
|
|
|
|
return BI.getOffsetDate(date, BI.Date._OFFSET[w < startOfWeek ? (7 + w - startOfWeek) : (w - startOfWeek)]); |
|
|
|
|
}, |
|
|
|
|
// 得到本周的结束日期
|
|
|
|
|
getWeekEndDate: function (date) { |
|
|
|
|
return getOffsetDate(date, _OFFSET[w < startOfWeek ? (7 + w - startOfWeek) : (w - startOfWeek)]); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 得到本周的结束日期 |
|
|
|
|
* @param date |
|
|
|
|
* @returns {Date} |
|
|
|
|
*/ |
|
|
|
|
export function getWeekEndDate(date) { |
|
|
|
|
var w = date.getDay(); |
|
|
|
|
var startOfWeek = BI.StartOfWeek % 7; |
|
|
|
|
return BI.getOffsetDate(date, BI.Date._OFFSET[w < startOfWeek ? (7 + w - startOfWeek) : (w - startOfWeek)] + 6); |
|
|
|
|
}, |
|
|
|
|
return getOffsetDate(date, _OFFSET[w < startOfWeek ? (7 + w - startOfWeek) : (w - startOfWeek)] + 6); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
getFullDayName: function (index) { |
|
|
|
|
return [BI.i18nText("BI-Basic_Sunday"), |
|
|
|
|
export function getFullDayName(index) { |
|
|
|
|
return [ |
|
|
|
|
BI.i18nText("BI-Basic_Sunday"), |
|
|
|
|
BI.i18nText("BI-Basic_Monday"), |
|
|
|
|
BI.i18nText("BI-Basic_Tuesday"), |
|
|
|
|
BI.i18nText("BI-Basic_Wednesday"), |
|
|
|
|
BI.i18nText("BI-Basic_Thursday"), |
|
|
|
|
BI.i18nText("BI-Basic_Friday"), |
|
|
|
|
BI.i18nText("BI-Basic_Saturday"), |
|
|
|
|
BI.i18nText("BI-Basic_Sunday")][index]; |
|
|
|
|
}, |
|
|
|
|
BI.i18nText("BI-Basic_Sunday") |
|
|
|
|
][index]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
getShortDayName: function (index) { |
|
|
|
|
return [BI.i18nText("BI-Basic_Simple_Sunday"), |
|
|
|
|
export function getShortDayName(index) { |
|
|
|
|
return [ |
|
|
|
|
BI.i18nText("BI-Basic_Simple_Sunday"), |
|
|
|
|
BI.i18nText("BI-Basic_Simple_Monday"), |
|
|
|
|
BI.i18nText("BI-Basic_Simple_Tuesday"), |
|
|
|
|
BI.i18nText("BI-Basic_Simple_Wednesday"), |
|
|
|
|
BI.i18nText("BI-Basic_Simple_Thursday"), |
|
|
|
|
BI.i18nText("BI-Basic_Simple_Friday"), |
|
|
|
|
BI.i18nText("BI-Basic_Simple_Saturday"), |
|
|
|
|
BI.i18nText("BI-Basic_Simple_Sunday")][index]; |
|
|
|
|
}, |
|
|
|
|
BI.i18nText("BI-Basic_Simple_Sunday") |
|
|
|
|
][index]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
getMonthName: function (index) { |
|
|
|
|
return [BI.i18nText("BI-Basic_January"), |
|
|
|
|
export function getMonthName(index) { |
|
|
|
|
return [ |
|
|
|
|
BI.i18nText("BI-Basic_January"), |
|
|
|
|
BI.i18nText("BI-Basic_February"), |
|
|
|
|
BI.i18nText("BI-Basic_March"), |
|
|
|
|
BI.i18nText("BI-Basic_April"), |
|
|
|
@ -193,18 +252,27 @@ BI._.extend(BI, {
|
|
|
|
|
BI.i18nText("BI-Basic_September"), |
|
|
|
|
BI.i18nText("BI-Basic_October"), |
|
|
|
|
BI.i18nText("BI-Basic_November"), |
|
|
|
|
BI.i18nText("BI-Basic_December")][index] |
|
|
|
|
}, |
|
|
|
|
BI.i18nText("BI-Basic_December") |
|
|
|
|
][index]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
getQuarterName: function (index) { |
|
|
|
|
return ["", BI.i18nText("BI-Quarter_1"), |
|
|
|
|
export function getQuarterName(index) { |
|
|
|
|
return [ |
|
|
|
|
"", |
|
|
|
|
BI.i18nText("BI-Quarter_1"), |
|
|
|
|
BI.i18nText("BI-Quarter_2"), |
|
|
|
|
BI.i18nText("BI-Quarter_3"), |
|
|
|
|
BI.i18nText("BI-Quarter_4")][index]; |
|
|
|
|
}, |
|
|
|
|
BI.i18nText("BI-Quarter_4") |
|
|
|
|
][index]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 格式化打印日期
|
|
|
|
|
print: function (date, str) { |
|
|
|
|
/** |
|
|
|
|
* 格式化打印日期 |
|
|
|
|
* @param date |
|
|
|
|
* @param str |
|
|
|
|
* @returns {*} |
|
|
|
|
*/ |
|
|
|
|
export function print(date, str) { |
|
|
|
|
var m = date.getMonth(); |
|
|
|
|
var d = date.getDate(); |
|
|
|
|
var y = date.getFullYear(); |
|
|
|
@ -212,23 +280,23 @@ BI._.extend(BI, {
|
|
|
|
|
while (yWith4number.length < 4) { |
|
|
|
|
yWith4number = "0" + yWith4number; |
|
|
|
|
} |
|
|
|
|
var wn = BI.getWeekNumber(date); |
|
|
|
|
var qr = BI.getQuarter(date); |
|
|
|
|
var wn = getWeekNumber(date); |
|
|
|
|
var qr = getQuarter(date); |
|
|
|
|
var w = date.getDay(); |
|
|
|
|
var s = {}; |
|
|
|
|
var hr = date.getHours(); |
|
|
|
|
var pm = (hr >= 12); |
|
|
|
|
var ir = (pm) ? (hr - 12) : hr; |
|
|
|
|
var dy = BI.getDayOfYear(date); |
|
|
|
|
if (ir == 0) { |
|
|
|
|
var dy = getDayOfYear(date); |
|
|
|
|
if (ir === 0) { |
|
|
|
|
ir = 12; |
|
|
|
|
} |
|
|
|
|
var min = date.getMinutes(); |
|
|
|
|
var sec = date.getSeconds(); |
|
|
|
|
s["%a"] = BI.getShortDayName(w); // abbreviated weekday name [FIXME: I18N]
|
|
|
|
|
s["%A"] = BI.getFullDayName(w); // full weekday name
|
|
|
|
|
s["%b"] = BI.Date._SMN[m]; // abbreviated month name [FIXME: I18N]
|
|
|
|
|
s["%B"] = BI.getMonthName(m); // full month name
|
|
|
|
|
s["%a"] = getShortDayName(w); // abbreviated weekday name [FIXME: I18N]
|
|
|
|
|
s["%A"] = getFullDayName(w); // full weekday name
|
|
|
|
|
s["%b"] = _SMN[m]; // abbreviated month name [FIXME: I18N]
|
|
|
|
|
s["%B"] = getMonthName(m); // full month name
|
|
|
|
|
// FIXME: %c : preferred date and time representation for the current locale
|
|
|
|
|
s["%C"] = 1 + Math.floor(y / 100); // the century number
|
|
|
|
|
s["%d"] = (d < 10) ? ("0" + d) : d; // the day of the month (range 01 to 31)
|
|
|
|
@ -309,4 +377,3 @@ BI._.extend(BI, {
|
|
|
|
|
|
|
|
|
|
return str; |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|