Comparing version 1.0.1 to 1.0.2
@@ -5,2 +5,3 @@ import * as moment from 'moment' | ||
export const TIME_FILTER_FORMAT: string = 'HH:mm' | ||
export const INPUT_DATE_FORMAT: string = 'YYYY-MM-DDTHH:mm' | ||
@@ -13,8 +14,8 @@ export const utcOffset = (): number => | ||
// moment parse the string itself | ||
const toMoment = (val: string) => moment(new Date(val)) | ||
export const toMoment = (val: string) => moment(new Date(val)) | ||
export const format = (val: string, fmt: string) => | ||
toMoment(val).format(fmt) | ||
moment(new Date(val)).format(fmt) | ||
export const formatSafe = (val: string | null | undefined, fmt: string) => | ||
export const formatSafe = (val: string|null|undefined, fmt: string) => | ||
typeof val === 'string' ? format(val, fmt) : '' | ||
@@ -32,6 +33,6 @@ | ||
export const diff = (a: string, b: string) => | ||
toMoment(a).diff(toMoment(b)) | ||
moment(new Date(a)).diff(moment(new Date(b))) | ||
export const diffNow = (val: string) => | ||
toMoment(val).diff(moment(new Date())) | ||
moment(new Date(val)).diff(moment(new Date())) | ||
@@ -42,2 +43,12 @@ export const toUtc = (val: string) => | ||
export const fromUtc = (val: string) => | ||
moment(new Date(val)).local().format() | ||
moment(new Date(val)).local().format() | ||
/** | ||
* Format required for datetime-local html input type | ||
* | ||
* @param value | ||
*/ | ||
export const formatDateForInput = (value: string) => { | ||
const formatted = moment(new Date(value)).format(INPUT_DATE_FORMAT) | ||
return formatted === 'Invalid date' ? value : formatted | ||
} |
@@ -6,5 +6,6 @@ "use strict"; | ||
exports.TIME_FILTER_FORMAT = 'HH:mm'; | ||
exports.INPUT_DATE_FORMAT = 'YYYY-MM-DDTHH:mm'; | ||
exports.utcOffset = () => moment.parseZone(new Date()).utcOffset(); | ||
const toMoment = (val) => moment(new Date(val)); | ||
exports.format = (val, fmt) => toMoment(val).format(fmt); | ||
exports.toMoment = (val) => moment(new Date(val)); | ||
exports.format = (val, fmt) => moment(new Date(val)).format(fmt); | ||
exports.formatSafe = (val, fmt) => typeof val === 'string' ? exports.format(val, fmt) : ''; | ||
@@ -14,6 +15,10 @@ exports.formatTime = (val) => exports.formatSafe(val, exports.TIME_FILTER_FORMAT); | ||
exports.formatMinutes = (time) => moment.duration(time, 'minute').asMinutes(); | ||
exports.diff = (a, b) => toMoment(a).diff(toMoment(b)); | ||
exports.diffNow = (val) => toMoment(val).diff(moment(new Date())); | ||
exports.diff = (a, b) => moment(new Date(a)).diff(moment(new Date(b))); | ||
exports.diffNow = (val) => moment(new Date(val)).diff(moment(new Date())); | ||
exports.toUtc = (val) => moment.utc(new Date(val)).format(); | ||
exports.fromUtc = (val) => moment(new Date(val)).local().format(); | ||
exports.formatDateForInput = (value) => { | ||
const formatted = moment(new Date(value)).format(exports.INPUT_DATE_FORMAT); | ||
return formatted === 'Invalid date' ? value : formatted; | ||
}; | ||
//# sourceMappingURL=datelib.js.map |
{ | ||
"name": "ts-datelib", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Typescript date formatting", | ||
@@ -5,0 +5,0 @@ "main": "dist/datelib.js", |
Sorry, the diff of this file is not supported yet
8251
93