- Notifications
You must be signed in to change notification settings - Fork0
A light and fast package to lookup timezones, get current datetime in any timezone, convert datetime timezones from utc to another timezone or vica-versa.
License
Anmol53/timezones
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A light and fast package to lookup timezones, get current datetime in any timezone, convert datetime timezones from utc to another timezone or vica-versa.
Just import the timezones package.
consttimezones=require("timezones")
- getAllTimezoneDetails()
- getTimezoneDetails()
- getOffset()
- convertFromUTC()
- convertToUTC()
- getCurrentDateTime()
- Will return all the available timezones, their abbreviation and their utc_offset.
- No Parameters
- Example:
constallTimezones=timezones.getAllTimezoneDetails();console.log(allTimezones);/*Output:[ { zone: 'Africa/Abidjan', abbreviation: 'GMT', utc_offset: '+00:00' }, { zone: 'Africa/Accra', abbreviation: 'GMT', utc_offset: '+00:00' }, { zone: 'Africa/Algiers', abbreviation: 'CET', utc_offset: '+01:00' }, { zone: 'Africa/Bissau', abbreviation: 'GMT', utc_offset: '+00:00' }, { zone: 'Africa/Cairo', abbreviation: 'EET', utc_offset: '+02:00' }, ... more items]*/
- Will return timezones, it's abbreviation and utc_offset.
- Parameters:
timezone
(required)
- Examples:
//Example 1consttimezoneDetails=timezones.getTimezoneDetails("Africa/Cairo");console.log(timezoneDetails);/*Output:{ status: 'OK', zone: 'Africa/Cairo', abbreviation: 'EET', utc_offset: '+02:00'}*/
//Example 2consttimezoneDetails=timezones.getTimezoneDetails("Abc");console.log(timezoneDetails);/*Output:{ status: 'Zone Not Found'}*/
//Example 3consttimezoneDetails=timezones.getTimezoneDetails();console.log(timezoneDetails);/*Output:{ status: 'Invalid call: timezone is required field'}*/
- Will return utc_offset of required timezone.
- Parameters:
timezone
(required)
- Examples:
//Example 1constoffset=timezones.getOffset("Asia/Oral");console.log(offset);/*Output:{ status: 'OK', utc_offset: '+05:00'}*/
//Example 2constoffset=timezones.getOffset("Abc");console.log(offset);/*Output:{ status: 'Zone Not Found'}*/
//Example 3constoffset=timezones.getOffset();console.log(offset);/*Output:{ status: 'Invalid call: timezone is required field'}*/
- Will return date (in ISO Format) in required timezone.
- Parameters:
date
(required) Must be a JS Dateutc_offset
zone
- Note: If both utc_offset and zone are not passed, function will just format
date
to ISO format and return it. - Examples:
//Example 1constrequired_date=timezones.convertFromUTC();console.log(required_date);/*Output:{ status: 'Invalid Date'}*/
//Example 2constrequired_date=timezones.convertFromUTC({date :"2022/11/06"});console.log(required_date);/*Output:{ status: 'Invalid Date'}*/// Reason: Date is not an JS Date
//Example 3constinputDate=newDate("Wed May 18 2022 21:38:17 GMT+0000");constrequired_date=timezones.convertFromUTC({date :inputDate});console.log(required_date);/*Output:{ status: 'OK', iso_date: '2022-05-18T21:38:17.000Z'}*/
//Example 4constinputDate=newDate("Wed May 18 2022 21:38:17 GMT+0000");constrequired_date=timezones.convertFromUTC({date :inputDate,utc_offset :"abc"});console.log(required_date);/*Output:{ status: 'Invalid Offset',}*/
//Example 5constinputDate=newDate("Wed May 18 2022 21:38:17 GMT+0000");constrequired_date=timezones.convertFromUTC({date :inputDate,utc_offset :"+05:30"});console.log(required_date);/*Output:{ status: 'OK', iso_date: '2022-05-19T03:08:17.000+05:30'}*/
//Example 6constinputDate=newDate("Wed May 18 2022 21:38:17 GMT+0000");constrequired_date=timezones.convertFromUTC({date :inputDate,zone :"Australia/Sydney"});console.log(required_date);/*Output:{ status: 'OK', iso_date: '2022-05-19T07:38:17.000+10:00'}*/
//Example 7constinputDate=newDate("Wed May 18 2022 21:38:17 GMT+0000");constrequired_date=timezones.convertFromUTC({date :inputDate,zone :"Australia/Sydney",utc_offset :"+09:00"});console.log(required_date);/*Output:{ status: 'utc_offset does not matched with zone'}*/
//Example 8constinputDate=newDate("Wed May 18 2022 21:38:17 GMT+0000");constrequired_date=timezones.convertFromUTC({date :inputDate,zone :"Australia/Sydney",utc_offset :"+10:00"});console.log(required_date);/*Output:{ status: 'OK', iso_date: '2022-05-19T07:38:17.000+10:00'}*/
- Will return date (in ISO Format) in UTC timezone from passed timezone.
- Parameters:
date
(required) Must be a JS Date
- Examples:
//Example 1constrequired_date=timezones.convertToUTC();console.log(required_date);/*Output:{ status: 'Invalid Date'}*/
//Example 2constrequired_date=timezones.convertToUTC({date :"2022/11/06"});console.log(required_date);/*Output:{ status: 'Invalid Date'}*/// Reason: Date is not an JS Date
//Example 3constinputDate=newDate("Wed May 18 2022 21:38:17 GMT+0530");constrequired_date=timezones.convertToUTC({date :inputDate});console.log(required_date);/*Output:{ status: 'OK', iso_date: '2022-05-18T16:08:17.000Z'}*/
- Will return date (in ISO Format) in required timezone.
- Parameters:
utc_offset
zone
- Note: If both utc_offset and zone are not passed, function will return UTC datetime.
- Examples:
//Example 1constrequired_date=timezones.getCurrentDateTime();console.log(required_date);/*Output:{ status: 'OK', iso_date: '2022-08-27T09:27:49.146Z'}*/
//Example 2constrequired_date=timezones.getCurrentDateTime({utc_offset :"abc"});console.log(required_date);/*Output:{ status: 'Invalid Offset',}*/
//Example 3constrequired_date=timezones.getCurrentDateTime({utc_offset :"+05:30"});console.log(required_date);/*Output:{ status: 'OK', iso_date: '2022-08-27T14:57:49.146+05:30'}*/
//Example 4constrequired_date=timezones.getCurrentDateTime({zone :"Australia/Sydney"});console.log(required_date);/*Output:{ status: 'OK', iso_date: '2022-08-27T19:27:49.146+10:00'}*/
//Example 5constrequired_date=timezones.getCurrentDateTime({zone :"Australia/Sydney",utc_offset :"+09:00"});console.log(required_date);/*Output:{ status: 'utc_offset does not matched with zone'}*/
//Example 6constrequired_date=timezones.getCurrentDateTime({zone :"Australia/Sydney",utc_offset :"+10:00"});console.log(required_date);/*Output:{ status: 'OK', iso_date: '2022-08-27T19:27:49.146+10:00'}*/
MIT License
Copyright (c) 2022 Anmol Agrawal
Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE.
Anmol Agrawal
Mail ID |LinkedIn |GitHub |LeetCode |CodeChef |HackerRank
"Any suggestions would be appreciated"