🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

str2date

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

str2date - npm Package Compare versions

Comparing version
0.1.1
to
1.1.1
+14
CHANGELOG.md
# Changelog
## 1.1.1
#### change
* 不再尝试将所有字符串转化为时间。无法转化的字符串不再转化为当前时间。
## 0.1.1
#### Copy from [parsetime-zhcn](!https://github.com/lln7777/parsetime-zhcn)
* 完善部分小bug,对于识别对象字符串,将尝试所有可能转化为时间格式
+54
-56

@@ -1,9 +0,9 @@

;(function (global, factory) {
; (function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define('str2date', factory) :
(global = global || self, (function () {
var current = global.str2date;
var exports = global.str2date = factory();
exports.noConflict = function () { global.str2date = current; return exports; };
}()));
typeof define === 'function' && define.amd ? define('str2date', factory) :
(global = global || self, (function () {
var current = global.str2date
var exports = global.str2date = factory()
exports.noConflict = function () { global.str2date = current; return exports }
}()))
}(this, (function () {

@@ -18,5 +18,5 @@ var timesDict = {

}
function str2date(str) {
var datetime, timeString;
var datetime, timeString
var now = new Date()

@@ -29,69 +29,67 @@ var yyyy = now.getFullYear()

var ss = now.getSeconds()
var beforeReg = /(\d+)\s*[^\s]{0,1}(年|个月|月|周|星期|天|小时|分|分钟|秒|秒钟)前/;
var justNowReg = /刚刚/;
var beforeMatches = str.match(beforeReg);
var beforeReg = /(\d+)\s*[^\s]{0,1}(年|个月|月|周|星期|天|小时|分|分钟|秒|秒钟)前/
var justNowReg = /刚刚/
var beforeMatches = str.match(beforeReg)
if (beforeMatches) {
var n = beforeMatches[1];
var unit = beforeMatches[2];
var n = beforeMatches[1]
var unit = beforeMatches[2]
if (timesDict[unit[0]]) {
datetime = new Date(Date.now() - n * timesDict[unit[0]]);
datetime = new Date(Date.now() - n * timesDict[unit[0]])
} else {
if (unit == '月' || unit == '个月') {
var _y = Math.floor(n / 12);
var _m = n % 12;
yyyy -= _y;
MM -= _m;
var _y = Math.floor(n / 12)
var _m = n % 12
yyyy -= _y
MM -= _m
if (MM < 1) {
yyyy -= 1;
MM += 12;
yyyy -= 1
MM += 12
}
} else if (unit == '年') {
yyyy -= n;
yyyy -= n
} else {
throw new Error('Parse Datetime String Error.');
throw new Error('Parse Datetime String Error.')
}
timeString = yyyy + '-' + MM + '-' + dd + ' ' + hh + ':' + mm + ':' + ss;
datetime = new Date(timeString);
timeString = yyyy + '-' + MM + '-' + dd + ' ' + hh + ':' + mm + ':' + ss
datetime = new Date(timeString)
}
} else if (justNowReg.test(str)) {
datetime = new Date();
datetime = new Date()
} else {
datetime = new Date(str);
datetime = new Date(str)
if (isNaN(datetime)) {
// 获取具体的日期
var zhcnDtReg = /(?:(\d{4})\s*年\s*)?(\d+)\s*月\s*(\d+)\s*(?:日|号)/i;
var zhcnDtRegMatch = str.match(zhcnDtReg);
if (zhcnDtRegMatch) {
if (zhcnDtRegMatch[3] !== undefined) dd = +zhcnDtRegMatch[3]
if (zhcnDtRegMatch[2] !== undefined) MM = +zhcnDtRegMatch[2]
if (zhcnDtRegMatch[1] !== undefined) yyyy = +zhcnDtRegMatch[1]
}
var zhcnDtReg = /(?:(\d{4})\s*年\s*)?(\d+)\s*月\s*(\d+)\s*(?:日|号)/i
var zhcnDtRegMatch = str.match(zhcnDtReg)
// 获取具体的时间分
var timeReg = /(am|pm|早上|下午|凌晨|晚上)?\s*(\d+)(?::|:)(\d+)(?:(?::|:)(\d+))?\s*(am|pm|早上|下午|凌晨|晚上)?/i;
var timeRegMatch = str.match(timeReg);
if (timeRegMatch) {
hh = +timeRegMatch[2];
mm = +timeRegMatch[3];
ss = timeRegMatch[4] ? timeRegMatch[4] : 0;
var pre = timeRegMatch[1] || timeRegMatch[5];
if (pre && ~['pm', '下午', '晚上'].indexOf(pre.toLowerCase()) && hh < 12) {
hh += 12;
var timeReg = /(am|pm|早上|下午|凌晨|晚上)?\s*(\d+)(?::|:)(\d+)(?:(?::|:)(\d+))?\s*(am|pm|早上|下午|凌晨|晚上)?/i
var timeRegMatch = str.match(timeReg)
if (zhcnDtRegMatch || timeRegMatch) {
if (zhcnDtRegMatch) {
if (zhcnDtRegMatch[3] !== undefined) dd = +zhcnDtRegMatch[3]
if (zhcnDtRegMatch[2] !== undefined) MM = +zhcnDtRegMatch[2]
if (zhcnDtRegMatch[1] !== undefined) yyyy = +zhcnDtRegMatch[1]
}
if (timeRegMatch) {
hh = +timeRegMatch[2]
mm = +timeRegMatch[3]
ss = timeRegMatch[4] ? timeRegMatch[4] : 0
var pre = timeRegMatch[1] || timeRegMatch[5]
if (pre && ~['pm', '下午', '晚上'].indexOf(pre.toLowerCase()) && hh < 12) {
hh += 12
}
}
timeString = yyyy + '-' + MM + '-' + dd + ' ' + hh + ':' + mm + ':' + ss
datetime = new Date(timeString)
}
timeString = yyyy + '-' + MM + '-' + dd + ' ' + hh + ':' + mm + ':' + ss;
datetime = new Date(timeString);
if (isNaN(datetime)) {
datetime = null;
}
}
}
return datetime;
return datetime
}
str2date.str2date = str2date;
str2date.str2date = str2date
return str2date
})));
})))
{
"name": "str2date",
"version": "0.1.1",
"version": "1.1.1",
"description": "将中文格式的时间字符串转换为Date类型",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -24,2 +24,6 @@ const str2date = require('./index');

console.log(str2date('1年前').toLocaleString())
console.log(str2date('刚刚').toLocaleString())
console.log(str2date('刚刚').toLocaleString())
console.log(str2date('xxxx').toLocaleString())
console.log(str2date('adfsadfdf').toLocaleString())
console.log(str2date('1231412').toLocaleString())