Comparing version 0.0.6 to 0.0.7
@@ -19,3 +19,9 @@ function testLog() { | ||
testLog(); | ||
testwritedir(); | ||
let a = `abc | ||
bcd | ||
aaaaa`; | ||
console.log(a); | ||
//testLog(); | ||
//testwritedir(); |
@@ -24,2 +24,17 @@ //这里将提供日期相关的工具函数 | ||
/** | ||
* 日期时间的常量 | ||
*/ | ||
const DatetimeConstant = { | ||
/** 最小的UTC时间 */ | ||
MIN_UTC : 1, | ||
/** 最大的UTC时间 */ | ||
MAX_UTC : 253370736000000, | ||
/** 无效的UTC时间 */ | ||
INVALID_UTC: 0, | ||
/** 每天最大的毫少数 */ | ||
MILLIS_PRE_DAY:86400000, | ||
}; | ||
let TimeZoneMinute = new Date().getTimezoneOffset(); //时区,单位,分 | ||
@@ -38,6 +53,8 @@ let TimeZoneSecond = TimeZoneMinute * 60; //时区,单位,秒 | ||
/** | ||
* 日期相关的工具类 | ||
* | ||
* @class dateUtils | ||
* @class datetimeUtils | ||
*/ | ||
@@ -50,3 +67,3 @@ class datetimeUtils { | ||
static version() { | ||
return 'datetimeUtils 1.1'; | ||
return 'datetimeUtils 1.2.0'; | ||
} | ||
@@ -63,3 +80,3 @@ /** | ||
* @return {number} 当前UTC时间戳 | ||
*/ | ||
*/ | ||
static getUTCNow() { | ||
@@ -78,3 +95,3 @@ return Date.now() + TimeZoneMillis; | ||
* @return {number} 当前UTC时间戳 | ||
*/ | ||
*/ | ||
static getUTCNowSecond() { | ||
@@ -125,3 +142,3 @@ return Math.floor((Date.now() + TimeZoneMillis)/1000); | ||
* | ||
* @memberOf dateUtils | ||
* @memberOf datetimeUtils | ||
*/ | ||
@@ -147,3 +164,3 @@ static dateString(date, millsFlag = true, daysplit = '-', timesplit = ':', millissplit = '.', datetimesplit = ' ') { | ||
* | ||
* @memberof dateUtils | ||
* @memberof datetimeUtils | ||
*/ | ||
@@ -164,13 +181,120 @@ static dateStringByFile(date, millisFlag = true) { | ||
* | ||
* @memberOf dateUtils | ||
* @memberOf datetimeUtils | ||
*/ | ||
static nowDateString(millsFlag = true) { | ||
return this.dateString(new Date(), millsFlag); | ||
} | ||
} | ||
/** | ||
* 判断是否是有效的时间戳 | ||
* @param {Number} paramUTC | ||
* @return {boolean} 判断结果 | ||
* - true 表示有效 | ||
* - false 表示无效 | ||
*/ | ||
static isValidUTC(paramUTC) { | ||
if(Number.isSafeInteger(paramUTC) && paramUTC >= DatetimeConstant.MIN_UTC && paramUTC <= DatetimeConstant.MAX_UTC) { | ||
return true; | ||
} | ||
else { | ||
return false; | ||
} | ||
} | ||
/** | ||
* 将指定日期的时间戳,转换为年月日-时分秒的字符串 | ||
* 格式为YYYY-MM-DD HH:mm:ss.ddd | ||
* | ||
* @static | ||
* @param {Number} paramUTC 要转的日期对象 | ||
* @param {boolean} [millsFlag=true] 是否带毫秒数 | ||
* @param {String} [daysplit='-'] 日期分隔符 | ||
* @param {String} [timesplit=':'] 时间分隔符 | ||
* @param {String} [millissplit='.'] 毫秒分隔符 | ||
* @param {String} [datetimesplit=' '] 日期和时间的分隔符 | ||
* @return {String} 转换后的字符串, 如果是无效时间戳,则返回null | ||
* | ||
* @memberOf datetimeUtils | ||
*/ | ||
static dateStringByUTC(paramUTC, millisFlag = true, daysplit = '-', timesplit = ':', millissplit = '.', datetimesplit = ' ') { | ||
if(!this.isValidUTC(paramUTC)) { | ||
return null; | ||
} | ||
return this.dateString(new Date(paramUTC), millisFlag, daysplit, timesplit, millissplit, datetimesplit); | ||
} | ||
/** | ||
* 将指定日期的时间,转换为年月日-时分秒的字符串 | ||
* 格式为YYYY-MM-DD HH:mm:ss.ddd | ||
* | ||
* @static | ||
* @param {Number} paramLocalTime 要转的日期对象 | ||
* @param {boolean} [millsFlag=true] 是否带毫秒数 | ||
* @param {String} [daysplit='-'] 日期分隔符 | ||
* @param {String} [timesplit=':'] 时间分隔符 | ||
* @param {String} [millissplit='.'] 毫秒分隔符 | ||
* @param {String} [datetimesplit=' '] 日期和时间的分隔符 | ||
* @return {String} 转换后的字符串, 如果是无效时间戳,则返回null | ||
* | ||
* @memberOf datetimeUtils | ||
*/ | ||
static dateStringByMillis(paramLocalTime, millisFlag = true, daysplit = '-', timesplit = ':', millissplit = '.', datetimesplit = ' ') { | ||
return this.dateStringByUTC(this.ToUTCTime(paramLocalTime), millisFlag, daysplit, timesplit, millissplit, datetimesplit); | ||
} | ||
/** | ||
* 将yyyy-mm-dd hh:mm:ss的时间日期字符串,转换为utc时间戳,单位毫秒数 | ||
* @param {string} paramDateTimeString 要解析的时间字符串 | ||
* @return {number} 返回结果, 0 表示失败,无效时间 | ||
*/ | ||
static DateTimeStringToUTC(paramDateTimeString) { | ||
let d = new Date(paramDateTimeString); | ||
let r = d.getTime(); | ||
if(!this.isValidUTC(r)) { | ||
return DatetimeConstant.INVALID_UTC; | ||
} | ||
else { | ||
return r; | ||
} | ||
} | ||
/** | ||
* 将UTC时间转换为本地时间 | ||
* @param {Number} paramUTC | ||
* @return {Number} | ||
*/ | ||
static ToLocalTime(paramUTC) { | ||
return paramUTC - TimeZoneMillis; | ||
} | ||
/** | ||
* 将本地时间转换为UTC时间 | ||
* @param {Number} paramLocalTime | ||
* @return {Number} | ||
*/ | ||
static ToUTCTime(paramLocalTime) { | ||
return paramLocalTime + TimeZoneMillis; | ||
} | ||
/** | ||
* 取当天0点时间的UTC时间 | ||
* @param {Number} paramUTC 当前的UTC时间,单位毫秒 | ||
* @return {Number} | ||
*/ | ||
static getTodayZeroTime(paramUTC) { | ||
let localTime = this.ToLocalTime(paramUTC); | ||
return this.ToUTCTime(localTime - localTime % DatetimeConstant.MILLIS_PRE_DAY); | ||
} | ||
/** | ||
* 判断是不是同一天 | ||
* @param {Number} paramUTC1 | ||
* @param {Number} paramUTC2 | ||
* @return {boolean} | ||
*/ | ||
static isSameDay(paramUTC1, paramUTC2) { | ||
let t1 = this.getTodayZeroTime(paramUTC1); | ||
let t2 = this.getTodayZeroTime(paramUTC2); | ||
return t1 === t2; | ||
} | ||
} | ||
exports = module.exports = { | ||
datetimeUtils | ||
datetimeUtils, | ||
DatetimeConstant, | ||
}; | ||
//exports.datetimeUtils = datetimeUtils; | ||
//exports.datetimeUtils = datetimeUtils; |
{ | ||
"name": "xmcommon", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "javascript common lib for es6", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -11,2 +11,6 @@ # xmcommon | ||
# 版本变更说明 | ||
## 0.07 | ||
- 2018-12-12 | ||
- \+ datetimeUtils 增加了ToLocalTime,ToUTCTime,getTodayZeroTime,isSameDay等函数 | ||
## 0.06 | ||
@@ -13,0 +17,0 @@ - 2018-11-01 |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
59790
17
1815
36