timezone-js-ts
Advanced tools
Comparing version 2.2.3 to 3.0.0
@@ -1,2 +0,18 @@ | ||
export declare function getCurrentGlobalTime(timezone: string, localeIdentifier?: string): string; | ||
export declare function getCurrentGlobalTime(timezone: string, localeIdentifier?: string): { | ||
time: string; | ||
timezone: any; | ||
diff: { | ||
dfference: any; | ||
yourLocalTimezone: string; | ||
}; | ||
difference?: undefined; | ||
} | { | ||
time: string; | ||
difference: { | ||
dfference: any; | ||
yourLocalTimezone: string; | ||
}; | ||
timezone?: undefined; | ||
diff?: undefined; | ||
}; | ||
export declare function allTimezones(): { | ||
@@ -3,0 +19,0 @@ id: number; |
@@ -47,3 +47,20 @@ "use strict"; | ||
const zoneData = __importStar(require("./countries-with-timezone.json")); | ||
function getCurrentGlobalTime(timezone, localeIdentifier) { | ||
// --------------------------------------------------------- | ||
// ------------------------- Helpers ---------------------- | ||
// --------------------------------------------------------- | ||
const monthArr = [ | ||
{ name: 'January', value: 1 }, | ||
{ name: 'Feburary', value: 2 }, | ||
{ name: 'March', value: 3 }, | ||
{ name: 'April', value: 4 }, | ||
{ name: 'May', value: 5 }, | ||
{ name: 'June', value: 6 }, | ||
{ name: 'July', value: 7 }, | ||
{ name: 'Augest', value: 8 }, | ||
{ name: 'September', value: 9 }, | ||
{ name: 'October', value: 10 }, | ||
{ name: 'November', value: 11 }, | ||
{ name: 'December', value: 12 }, | ||
]; | ||
function timeNowIs(timezone) { | ||
const date = new Date(); | ||
@@ -55,22 +72,81 @@ const configOptions = { | ||
}; | ||
const res = new Intl.DateTimeFormat(localeIdentifier ? localeIdentifier : 'en-GB', configOptions).format(date); | ||
const splittedRes = res.split(' '); | ||
if (splittedRes[splittedRes.length - 1] !== getTimezoneName(timezone)) { | ||
return `${res} ${getTimezoneName(timezone)}`; | ||
return new Intl.DateTimeFormat('en-GB', configOptions).formatToParts(date); | ||
} | ||
function getTimezoneName(data) { | ||
const dateObj = timeNowIs(data); | ||
const findTz = dateObj.find(z => z.type === 'timeZoneName'); | ||
var tzStr = findTz.value; | ||
return tzStr; | ||
} | ||
function getTimeDiff(timezone) { | ||
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m; | ||
let timeDiff; | ||
const currTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone; | ||
const destTime = timeNowIs(timezone); | ||
let destHour = (_a = destTime.find(h => h.type == 'hour')) === null || _a === void 0 ? void 0 : _a.value; | ||
let destMinute = (_b = destTime.find(h => h.type == 'minute')) === null || _b === void 0 ? void 0 : _b.value; | ||
const destSecond = (_c = destTime.find(h => h.type == 'second')) === null || _c === void 0 ? void 0 : _c.value; | ||
const destDay = (_d = destTime.find(h => h.type == 'day')) === null || _d === void 0 ? void 0 : _d.value; | ||
let destMonth = (_e = destTime.find(h => h.type == 'month')) === null || _e === void 0 ? void 0 : _e.value; | ||
const destYear = (_f = destTime.find(h => h.type == 'year')) === null || _f === void 0 ? void 0 : _f.value; | ||
const currTime = timeNowIs(currTimeZone); | ||
let currHour = (_g = currTime.find(h => h.type == 'hour')) === null || _g === void 0 ? void 0 : _g.value; | ||
let currMinute = (_h = currTime.find(h => h.type == 'minute')) === null || _h === void 0 ? void 0 : _h.value; | ||
const currSecond = (_j = currTime.find(h => h.type == 'second')) === null || _j === void 0 ? void 0 : _j.value; | ||
const currDay = (_k = currTime.find(h => h.type == 'day')) === null || _k === void 0 ? void 0 : _k.value; | ||
let currMonth = (_l = currTime.find(h => h.type == 'month')) === null || _l === void 0 ? void 0 : _l.value; | ||
const currYear = (_m = currTime.find(h => h.type == 'year')) === null || _m === void 0 ? void 0 : _m.value; | ||
currMonth = monthArr.find((m) => m.name == currMonth).value; | ||
destMonth = monthArr.find((m) => m.name == destMonth).value; | ||
const currDateTimeSmplFrmt = `${currDay}-${currMonth}-${currYear} ${currHour}:${currMinute}:${currSecond}`; | ||
const destDateTimeSmplFrmt = `${destDay}-${destMonth}-${destYear} ${destHour}:${destMinute}:${destSecond}`; | ||
if (currDateTimeSmplFrmt > destDateTimeSmplFrmt) { | ||
let minute = currMinute - destMinute; | ||
if (currHour == '00') { | ||
currHour = 24; | ||
} | ||
if (currMinute < destMinute) { | ||
minute = destMinute - currMinute; | ||
} | ||
const diff = `${currHour - destHour}:${minute}`; | ||
timeDiff = `${diff} hour(s) behind`; | ||
} | ||
else { | ||
return res; | ||
else if (currDateTimeSmplFrmt == destDateTimeSmplFrmt) { | ||
const diff = `${currHour - destHour}:${currMinute - destMinute}`; | ||
timeDiff = `same as <name_of_the_place>`; | ||
} | ||
else if (currDateTimeSmplFrmt < destDateTimeSmplFrmt) { | ||
let minute = destMinute - currMinute; | ||
if (destHour == '00') { | ||
destHour = 24; | ||
} | ||
if (destMinute < currMinute) { | ||
minute = currMinute - destMinute; | ||
} | ||
const diff = `${destHour - currHour}:${minute}`; | ||
timeDiff = `${diff} hour(s) ahead`; | ||
} | ||
return { dfference: timeDiff, yourLocalTimezone: currTimeZone }; | ||
} | ||
function getTimezoneName(data) { | ||
// --------------------------------------------------------- | ||
// ------------------------- Features ---------------------- | ||
// --------------------------------------------------------- | ||
function getCurrentGlobalTime(timezone, localeIdentifier) { | ||
const date = new Date(); | ||
const configOptions = { | ||
timeZone: data, | ||
timeZone: timezone, | ||
dateStyle: 'full', | ||
timeStyle: 'long' | ||
}; | ||
const dateObj = new Intl.DateTimeFormat('en-US', configOptions).formatToParts(date); | ||
const findTz = dateObj.find(z => z.type === 'timeZoneName'); | ||
var tzStr = findTz.value; | ||
return tzStr; | ||
const res = new Intl.DateTimeFormat(localeIdentifier ? localeIdentifier : 'en-GB', configOptions).format(date); | ||
const splittedRes = res.split(' '); | ||
const diff = getTimeDiff(timezone); | ||
if (splittedRes[splittedRes.length - 1] !== getTimezoneName(timezone)) { | ||
return { time: res, timezone: getTimezoneName(timezone), diff }; | ||
// return `${res} ${getTimezoneName(timezone)}`; | ||
} | ||
else { | ||
return { time: res, difference: diff }; | ||
// return res; | ||
} | ||
} | ||
@@ -81,3 +157,2 @@ function allTimezones() { | ||
function timezonesByPlace(place) { | ||
// const place = 'los angeles'; | ||
const cities = citiesData; | ||
@@ -84,0 +159,0 @@ const states = statesData; |
{ | ||
"name": "timezone-js-ts", | ||
"version": "2.2.3", | ||
"version": "3.0.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
11587639
10911