date-utils-2020
Advanced tools
Comparing version 1.0.0 to 1.0.1
/*! For license information please see date-utils-2020.js.LICENSE.txt */ | ||
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var r=t();for(var n in r)("object"==typeof exports?exports:e)[n]=r[n]}}("undefined"!=typeof self?self:this,(function(){return(()=>{"use strict";var e={949:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.toTwoDigits=t.isString=t.isNumberLike=void 0,t.isNumberLike=function(e){return"number"==typeof e||"string"==typeof e&&/^-?(\d+|\d+\.\d+)$/.test(e)},t.isString=function(e){return"string"==typeof e},t.toTwoDigits=function(e){var t=e+"";return t[1]?t:"0"+t}},607:(e,t,r)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.toTwoDigits=t.toDate=t.isNumberLike=t.formatDate=void 0;var n=r(949);Object.defineProperty(t,"isNumberLike",{enumerable:!0,get:function(){return n.isNumberLike}}),Object.defineProperty(t,"toTwoDigits",{enumerable:!0,get:function(){return n.toTwoDigits}});var i={weeks:["Sun","Mon","Tue","Wed","Thu","Fri","Sat"]};function o(e){if(!e)return null;if(e instanceof Date)return e;var t=e+"";if(n.isNumberLike(t)){var r=t.length;return 8===r?new Date([t.substr(0,4),t.substr(4,2),t.substr(6,2)].join("/")):6===r?new Date([t.substr(0,4),t.substr(4,2),"01"].join("/")):4===r?new Date(t+"/01/01"):new Date(t)}if(t=(t=t.replace(/[年月日]/g,(function(e){return"日"===e?"":"/"}))).replace(/[(((].*?[)))]/g," ").replace(/\bam|pm\b/gi," ").replace(/\s+/g," "),/^(\d{4})[-/](\d{1,2})[-/](\d{1,2})$/.test(t))return new Date([RegExp.$1,RegExp.$2,RegExp.$3].join("/"));if(/^(\d{4})[-/](\d{1,2})$/.test(t))return new Date([RegExp.$1,RegExp.$2,"01"].join("/"));var i=new Date(t);return isNaN(i.getFullYear())?null:i}t.formatDate=function(e,t,r){var u,s=o(e);if(!s||!t)return e+"";if("timestamp"===t)return s.getTime().toString();/(y+)/i.test(t)&&(u=RegExp.$1,t=t.replace(u,(s.getFullYear()+"").substr(4-u.length))),r&&Array.isArray(r.weeks)||(r=i);var a={"M+":s.getMonth()+1,"d+":s.getDate(),"h+":s.getHours(),"m+":s.getMinutes(),"s+":s.getSeconds(),"w+":s.getDay(),"W+":r.weeks[s.getDay()],"a+":s.getHours()<12?"am":"pm","A+":s.getHours()<12?"AM":"PM"};for(var f in a)if(new RegExp("("+f+")").test(t)){u=RegExp.$1;var g=a[f]+"";t=t.replace(u,1===u.length?g:n.toTwoDigits(g))}return t},t.toDate=o}},t={};return function r(n){if(t[n])return t[n].exports;var i=t[n]={exports:{}};return e[n](i,i.exports,r),i.exports}(607)})()})); | ||
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var r=t();for(var n in r)("object"==typeof exports?exports:e)[n]=r[n]}}("undefined"!=typeof self?self:this,(function(){return(()=>{"use strict";var e={949:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.toTwoDigits=void 0,t.toTwoDigits=function(e){return e[1]?e:"0"+e}},607:(e,t,r)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.toDate=t.formatDate=void 0;var n=r(949),o={weeks:["Sun","Mon","Tue","Wed","Thu","Fri","Sat"]};function i(e){if(e instanceof Date)return e;if("number"==typeof e)return new Date(e);if("string"==typeof e){var t=e.trim();if(/^\d+$/.test(t)){var r=t.length;return 8===r?new Date([t.substr(0,4),t.substr(4,2),t.substr(6,2)].join("/")):6===r?new Date([t.substr(0,4),t.substr(4,2),"01"].join("/")):4===r?new Date(t+"/01/01"):new Date(parseInt(e))}if(t=t.replace(/[年月日]/g,(function(e){return"日"===e?"":"/"})).replace(/[(((].*?[)))]/g," ").replace(/\bam|pm\b/gi," ").replace(/\s+/g," "),/^(\d{4})[-/](\d{1,2})[-/](\d{1,2})$/.test(t))return new Date([RegExp.$1,RegExp.$2,RegExp.$3].join("/"));if(/^(\d{4})[-/](\d{1,2})$/.test(t))return new Date([RegExp.$1,RegExp.$2,"01"].join("/"));var n=new Date(t);return isNaN(n.getFullYear())?null:n}return null}t.formatDate=function(e,t,r){var s,a=i(e);if(!a||!t)return e+"";if("timestamp"===t)return a.getTime().toString();/(y+)/i.test(t)&&(s=RegExp.$1,t=t.replace(s,(a.getFullYear()+"").substr(4-s.length))),r&&Array.isArray(r.weeks)||(r=o);var u={"M+":a.getMonth()+1,"d+":a.getDate(),"h+":a.getHours(),"m+":a.getMinutes(),"s+":a.getSeconds(),"w+":a.getDay(),"W+":r.weeks[a.getDay()],"a+":a.getHours()<12?"am":"pm","A+":a.getHours()<12?"AM":"PM"};for(var f in u)if(new RegExp("("+f+")").test(t)){s=RegExp.$1;var p=u[f]+"";t=t.replace(s,1===s.length?p:n.toTwoDigits(p))}return t},t.toDate=i}},t={};return function r(n){if(t[n])return t[n].exports;var o=t[n]={exports:{}};return e[n](o,o.exports,r),o.exports}(607)})()})); |
/*! | ||
* date-utils-2020 v1.0.0 | ||
* date-utils-2020 v1.0.1 | ||
* Author: Capricorncd | ||
* Repository: https://github.com/capricorncd/date-utils-2020#readme | ||
* Released on: 2021-01-01 21:39:26 | ||
* Released on: 2021-01-02 21:48:43 | ||
*/ |
{ | ||
"name": "date-utils-2020", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "date utils 2020, format(date: Date, formatter: string)/toDate(a?: any)...", | ||
"main": "dist/date-utils-2020.js", | ||
"scripts": { | ||
"test": "node test/index.js", | ||
"test": "ts-node test/index.ts", | ||
"dev": "webpack serve", | ||
@@ -47,2 +47,3 @@ "build": "webpack --mode production" | ||
"ts-loader": "^8.0.13", | ||
"ts-node": "^9.1.1", | ||
"typescript": "^4.1.3", | ||
@@ -49,0 +50,0 @@ "webpack": "^5.9.0", |
# date-utils-2020 | ||
date utils 2020, format(date: Date, formatter: string)/toDate(a?: any)... | ||
date utils 2020, format(s: any, formatter: string)/toDate(s?: any)... | ||
> Note: | ||
> When the parameter `s` is a string: when it is a string of `yyyyMMdd`/`yyyyMM`/`yyyy`, it is processed as `yyyyMMdd`/`yyyyMM01`/`yyyy0101`, and other string numbers are processed as a `timestamp`. | ||
<p align="left"> | ||
@@ -28,3 +32,3 @@ <a href="https://npmcharts.com/compare/date-utils-2020?minimal=true"><img src="https://img.shields.io/npm/dm/date-utils-2020.svg?sanitize=true" alt="Downloads"></a> | ||
console.log(result) | ||
// 2020/12/04 五 | ||
// 2020/12/04 Fri | ||
``` | ||
@@ -39,2 +43,10 @@ | ||
// 2020/12/05 16:07:43 | ||
// timestamp | ||
console.log(formatDate(20210101, 'yyyy-MM-dd hh:mm:ss')) | ||
// 1970-01-01 14:36:50 | ||
// yyyyMMdd | ||
console.log(formatDate('20210101', 'yyyy-MM-dd hh:mm:ss')) | ||
// 2021-01-01 00:00:00 | ||
``` | ||
@@ -52,12 +64,10 @@ | ||
|formatDate|(date: any, format: string, langPackage?: ILangPackage)|return `string`| | ||
|toDate|(s: any)|return `Date` or `null`| | ||
|isNumberLike|(n: any)| return boolean `true` or `false`| | ||
|toTwoDigits|(n: number)| return `string`| | ||
|toDate|(s: any)|return `Date` or `null`. When the parameter `s` is a string: when it is a string of `yyyyMMdd`/`yyyyMM`/`yyyy`, it is processed as `yyyyMMdd`/`yyyyMM01`/`yyyy0101`, and other string numbers are processed as a `timestamp`.| | ||
#### format | ||
Date Formats | ||
Date Formats, Example `yyyy-MM-dd hh:mm:ss W` => `2021-01-01 01:01:01 Fri` | ||
|format|meaning|Example| | ||
|:--|:--|:--|:--| | ||
|:--|:--|:--| | ||
|yyyy/yy|year|`2021`/`21`| | ||
@@ -64,0 +74,0 @@ |MM/M|month|`01`/`1`| |
@@ -6,29 +6,9 @@ /** | ||
*/ | ||
/** | ||
* is number like | ||
* @param n | ||
* @returns {boolean} | ||
*/ | ||
export function isNumberLike<T>(n: T): boolean { | ||
return typeof n === 'number' || (typeof n === 'string' && /^-?(\d+|\d+\.\d+)$/.test(n)) | ||
} | ||
/** | ||
* is string | ||
* @param s | ||
* @returns {boolean} | ||
*/ | ||
export function isString<T>(s: T): boolean { | ||
return typeof s === 'string' | ||
} | ||
/** | ||
* to two digits | ||
* @param n | ||
* @param str | ||
* @returns {string} | ||
*/ | ||
export function toTwoDigits<T>(n: T): string { | ||
const str = n + '' | ||
export function toTwoDigits(str: string): string { | ||
return str[1] ? str : '0' + str | ||
} |
@@ -6,3 +6,3 @@ /** | ||
*/ | ||
import { isNumberLike, toTwoDigits } from './helper' | ||
import { toTwoDigits } from './helper' | ||
import * as Types from '../types/index' | ||
@@ -22,3 +22,3 @@ | ||
*/ | ||
function formatDate<T>(srcDate: T, fmt: string, langPackage: Types.ILangPackage): string { | ||
function formatDate<T>(srcDate: T, fmt: string, langPackage?: Types.ILangPackage): string { | ||
const date = toDate(srcDate) | ||
@@ -69,49 +69,55 @@ if (!date || !fmt) return srcDate + '' | ||
function toDate<T>(input: T): null | Date { | ||
if (!input) return null | ||
if (input instanceof Date) return input | ||
let str = input + '' | ||
if (isNumberLike(str)) { | ||
const len = str.length | ||
// yyyyMMdd | ||
if (len === 8) { | ||
return new Date([str.substr(0, 4), str.substr(4, 2), str.substr(6, 2)].join('/')) | ||
} | ||
// yyyyMM | ||
else if (len === 6) { | ||
return new Date([str.substr(0, 4), str.substr(4, 2), '01'].join('/')) | ||
} | ||
// yyyy | ||
else if (len === 4) { | ||
return new Date(str + '/01/01') | ||
} | ||
// Other cases are handled as timestamp | ||
else { | ||
return new Date(str) | ||
} | ||
} else { | ||
// replace 年月日 | ||
str = str.replace(/[年月日]/g, (match) => { | ||
return match === '日' ? '' : '/' | ||
}) | ||
// remove cn/jp week, comment | ||
// 2020/08/22(星期六) 11:56:21 | ||
// Sat Aug 22 2020 11:56:24 GMT+0900 (Japan Standard Time) | ||
str = str.replace(/[(((].*?[)))]/g, ' ') | ||
.replace(/\bam|pm\b/ig, ' ') | ||
.replace(/\s+/g, ' ') | ||
/** yyyy/MM/dd yyyy-MM-dd */ | ||
if (/^(\d{4})[-/](\d{1,2})[-/](\d{1,2})$/.test(str)) { | ||
return new Date([RegExp.$1, RegExp.$2, RegExp.$3].join('/')) | ||
} | ||
/** yyyy/MM yyyy-MM */ | ||
else if (/^(\d{4})[-/](\d{1,2})$/.test(str)) { | ||
return new Date([RegExp.$1, RegExp.$2, '01'].join('/')) | ||
// fix: In the case of an array with only one element | ||
// Example: ['2021/01/02'].toString() => '2021/01/02' | ||
if (typeof input === 'number') { | ||
return new Date(input) | ||
} else if (typeof input === 'string') { | ||
let str = input.trim() | ||
// string number | ||
if (/^\d+$/.test(str)) { | ||
const len = str.length | ||
// yyyyMMdd | ||
if (len === 8) { | ||
return new Date([str.substr(0, 4), str.substr(4, 2), str.substr(6, 2)].join('/')) | ||
} | ||
// yyyyMM | ||
else if (len === 6) { | ||
return new Date([str.substr(0, 4), str.substr(4, 2), '01'].join('/')) | ||
} | ||
// yyyy | ||
else if (len === 4) { | ||
return new Date(str + '/01/01') | ||
} | ||
// Other cases are handled as timestamp | ||
else { | ||
// Note that the results of new Date(0) and new Date('0') are different | ||
return new Date(parseInt(input)) | ||
} | ||
} else { | ||
const date = new Date(str) | ||
if (isNaN(date.getFullYear())) { | ||
return null | ||
// replace 年月日 | ||
str = str | ||
.replace(/[年月日]/g, (match) => { | ||
return match === '日' ? '' : '/' | ||
}) | ||
// remove cn/jp week, comment | ||
// 2020/08/22(星期六) 11:56:21 | ||
// Sat Aug 22 2020 11:56:24 GMT+0900 (Japan Standard Time) | ||
.replace(/[(((].*?[)))]/g, ' ') | ||
.replace(/\bam|pm\b/ig, ' ') | ||
.replace(/\s+/g, ' ') | ||
/** yyyy/MM/dd yyyy-MM-dd */ | ||
if (/^(\d{4})[-/](\d{1,2})[-/](\d{1,2})$/.test(str)) { | ||
return new Date([RegExp.$1, RegExp.$2, RegExp.$3].join('/')) | ||
} | ||
return date | ||
/** yyyy/MM yyyy-MM */ | ||
else if (/^(\d{4})[-/](\d{1,2})$/.test(str)) { | ||
return new Date([RegExp.$1, RegExp.$2, '01'].join('/')) | ||
} else { | ||
const date = new Date(str) | ||
return isNaN(date.getFullYear()) ? null : date | ||
} | ||
} | ||
} | ||
return null | ||
} | ||
@@ -121,5 +127,3 @@ | ||
formatDate, | ||
isNumberLike, | ||
toDate, | ||
toTwoDigits | ||
toDate | ||
} |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
15853
96
0
23
281