@time-zone/date
v2.2.2
Published
让代码运行在不同时区(国家)环境下,都能准确获得指定时区(国家)的时间!Let the code run in different time zones (countries) environment, can accurately obtain the time of the specified time zone (country)!
Downloads
151
Maintainers
Readme
Get the tool library of time zones and times in the world
Preface
- Let the code run in different national environments, you can accurately obtain the time of the specified country!
English | 简体中文
Installation
npm i @time-zone/date --save
Example
- All APIs are introduced in the same way
- Total 31 APIs
import {isUS_Wt,isUS_Dst,getWorldTimeZone,getUTCDate} from '@time-zone/date'
isUS_Wt() // true: It's winter time in the United States GMT-5 ; false: You know...
getUTCDate() // UTC Current time
getUTCDate("2022-2-1")
getUTCDate("2022-2-1 13:10:10")
getWorldTimeZone()
getWorldTimeZone("2022-2-1 13:10:10",'en-US')
getWorldTimeZone("2022-2-1 13:10:10",'en-US',{timeZone:'America/New_York'}) // Specify the time corresponding to a time zone
getWorldTimeZone(null,'en-US',{timeZone:'America/New_York'}) // Time zone current time
getWorldTimeZone(new Date(),'en-US',{timeZone:'America/New_York'}) // Time zone current time
getWorldTimeZone(1643692210000)
Referral function
Determine whether the United States is now in winter time
Judge whether the United States is now daylight saving time
Gets the time in the specified time zone
- Full time
- Full time format adjustment
- yyyymmdd | ddmmyyyy | mmddyyyy
- Custom year and month and date formats: order, separator
- hhmmss
- year
- month
- date
- hours(24-hour system)
- minutes
- seconds
- week
In addition, the above method can replace all the following contents
- Get EST time zone
- Full time
- yyyymmdd
- hhmmss
- yearv
- month
- date
- hours(24-hour system. You can set 0 as the starting point of a new day or 24 as the starting point of a new day)
- minutes
- seconds
- week
- Get UTC time zone: time difference ± 0
- Full time
- year
- month
- date
- hours
- minutes
- seconds
API
getWorldTimeZone()
As mentioned at the beginning of the introduction, this is the main API .
See the end of the document for references tolocales
andoptions
parameters.
- Gets the time in the specified time zone. The first parameter is the returned language type and the second parameter is the target time zone
- 3 parameters
date
- purpose:
Specify the time to get, and the default current time will not be transferred
- type:
?:string | number | Date | null | undefined
- value:
Those that match the first parameter of 'new date()' can be used, and 'null' and 'undefined' represent the current time.
- purpose:
locales
- purpose:
Returns the corresponding language type
- type:
?:string
- value:
language[-scripts][-region]
- purpose:
options
- purpose:
Adjust the returned time format content. Specifies the time to return to the time zone
- type:
?:object
- value:
See options parameter below
- purpose:
import { getWorldTimeZone } from '@time-zone/date'
// Get the full time of the target time zone
let date = getWorldTimeZone(new Date(),'en-US',{timeZone:'America/New_York'})
// If it is not transmitted, the current time zone of the code running environment will be the default
// getWorldTimeZone()
custom_yyyymmdd_sort()
Get the month, year and day of the specified time zone, and you can customize the order of month, year and day (supplement 0 when month and day are single digits)
- 4 parameters
timeZone
:typestring
- Date sequence:type
string
- 1、Must be lowercase。
- 2、The letter is :
y
、m
、d
。 - 3、The default value for parameters not transmitted isyyyymmdd
- The order of 3 letters determines the order of return
- Separator, not passed, default to
/
- Specified time: the default current time is not transmitted; Parameter type is
?:string | number | Date | null | undefined
import { custom_yyyymmdd_sort } from '@time-zone/date'
let ymd = custom_yyyymmdd_sort('Asia/Shanghai','ymd','-',new Date())
let dmy = custom_yyyymmdd_sort('Asia/Shanghai','dmy','/')
let ydm = custom_yyyymmdd_sort('Asia/Shanghai','ydm','*-@#$$%^^*&(*&)(_+`~')
let mdy = custom_yyyymmdd_sort('Asia/Shanghai','mdy')
// The month, year and day can be combined arbitrarily, and if there is a repetition, the returned result will also appear repeatedly
let ymdy = custom_yyyymmdd_sort('Asia/Shanghai','ymdy')
get_mmddyyyy() get_ddmmyyyy() get_yyyymmdd()
Get the month, day and year of the specified time zone (supplement 0 if the month and day are single digits)
- 3 Parameters
timeZone
:typestring
- Separator, not passed, default to
/
- Specified time: the default current time is not transmitted; Parameter type is
?:string | number | Date | null | undefined
import { get_mmddyyyy,get_ddmmyyyy,get_yyyymmdd } from '@time-zone/date'
let mdy = get_mmddyyyy('Asia/Shanghai','/', new Date())
// get_ddmmyyyy() , get_yyyymmdd()
// No explanation, just look at the name to know the order.
custom_ymd_sort()
- 0 will not be added if the month day is a single digit
- Other contents follow
custom_ yyyymmdd_ Sort()
consistent, no further explanation
import { custom_ymd_sort } from '@time-zone/date'
let ymd = custom_ymd_sort( 'Asia/Shanghai','ymd','-',new Date() )
get_hms()
- Gets the hour, minute, and second of the target time zone
- 2 parameters
timeZone
:typestring
- Specified time: the default current time is not transmitted; Parameter type is
?:string | number | Date | null | undefined
import { get_hms } from '@time-zone/date'
let hms = get_hms('Asia/Shanghai') // format:08:00:00
getYear()
- Gets the year of the target time zone
- 2 parameters
timeZone
:typestring
- Specified time: the default current time is not transmitted; Parameter type is
?:string | number | Date | null | undefined
import { getYear } from '@time-zone/date'
let year = getYear('Asia/Shanghai') // Return value number type
getMonth()
- Gets the month (0-11) of the target time zone
- 2 parameters
timeZone
:typestring
- Specified time: the default current time is not transmitted; Parameter type is
?:string | number | Date | null | undefined
import { getMonth } from '@time-zone/date'
let month = getMonth('Asia/Shanghai') // Return value number type
getDate()
- Gets the day of the target time zone
- 2 parameters
timeZone
:typestring
- Specified time: the default current time is not transmitted; Parameter type is
?:string | number | Date | null | undefined
import { getDate } from '@time-zone/date'
let date = getDate('Asia/Shanghai') // Return value number type
getHours()
- Gets the hour of the target time zone
- Get the hours of the target time zone separately, and the unified return value is: '0-23', so as to avoid confusion caused by the problem of 1-24 in the United States.
- 2 parameters
timeZone
:typestring
- Specified time: the default current time is not transmitted; Parameter type is
?:string | number | Date | null | undefined
import { getHours } from '@time-zone/date'
let hours = getHours('Asia/Shanghai') // Return value number type
getMinutes()
- Gets the minutes of the target time zone
- 2 parameters
timeZone
:typestring
- Specified time: the default current time is not transmitted; Parameter type is
?:string | number | Date | null | undefined
import { getMinutes } from '@time-zone/date'
let minutes = getMinutes('Asia/Shanghai') // Return value number type
getSeconds()
- Gets the second of the target time zone
- 2 parameters
timeZone
:typestring
- Specified time: the default current time is not transmitted; Parameter type is
?:string | number | Date | null | undefined
import { getSeconds } from '@time-zone/date'
let sec = getSeconds('Asia/Shanghai') // Return value number type
getDay()
- Gets the week in the target time zone
- The return value is' 0-6 ', 0 is Sunday, 1 is Monday, followed by order
- 2 parameters
timeZone
:typestring
- Specified time: the default current time is not transmitted; Parameter type is
?:string | number | Date | null | undefined
import { getDay } from '@time-zone/date'
let day = getDay('Asia/Shanghai') // Return value number type
getESTDate()
- Get the time of EST time zone. The returned language type is en US
- parameter
- Specified time: the default current time is not transmitted; Parameter type is
?:string | number | Date | null | undefined
- Specified time: the default current time is not transmitted; Parameter type is
import { getESTDate } from '@time-zone/date'
let date = getESTDate()
getUTCDate()
- Get UTC time zone
- parameter
- Specified time: the default current time is not transmitted; Parameter type is
?:string | number | Date | null | undefined
- Specified time: the default current time is not transmitted; Parameter type is
import { getUTCDate } from '@time-zone/date'
let date = getUTCDate()
isUS_Dst()
Judge whether the United States is now daylight saving time
- No parameters.
- Returning 'true' indicates that the United States is currently daylight saving time
- Return value:
true
|false
import { isUS_Dst } from '@time-zone/date'
let booleanDate = isUS_Dst()
isUS_Wt()
Determine whether the United States is now in winter time
- No parameters.
- Returning 'true' indicates that it is winter time in the United States
- Return value:
true
|false
import { isUS_Wt } from '@time-zone/date'
let booleanDate = isUS_Wt()
Other
Corresponding value of EST time
- parameter
- Specified time: the default current time is not transmitted; Parameter type is
?:string | number | Date | null | undefined
- Specified time: the default current time is not transmitted; Parameter type is
import { getESTmmddyyyy, ...... } from '@time-zone/date'
getESTmmddyyyy()
getESThms()
EST_getYear()
EST_getMonth()
EST_getDate()
EST_getHours(0) // U.S. hours. If 0 is passed, it means that the starting point of the new day is 0, otherwise it is 24
EST_getMinutes()
EST_getSeconds()
EST_getDay()
locales
parameter
- type:
string
- language[-scripts][-region]
- The special code can be replaced by the corresponding code type. Refer to the MDN website for details
- language[-scripts][-region]-u-nu-*
- language[-scripts][-region]-u-ca-*
- reference value
- Refer to for other values:http://www.lingoes.cn/zh/translator/langcode.htm
"zh-Hans" //Simplified Chinese
"zh-TW" //Traditional Chinese
"ru-RU" //Russian
"en-GB" //britain
"it-IT" //Italy
"fr-FR" //French
"pt-PT" //Portugal
"de-DE" //German
"en-US" //usa
"en-AU" //澳大利亚
"en-CA" //Australia
"ko-KR" //the republic of korea
"ar-EG" //arab
"ja-JP" //Japan
"nl-NL" //Netherlands
"es-ES" //Spain
"hi-IN" //Hindi
"eo" //Esperanto
options
parameter
type:
object
The built-in default parameter is
const DEFAULT_OPTIONS = {
year: "numeric",
month: "numeric",
day: "numeric",
hour: "numeric",
minute: "numeric",
second: "numeric",
weekday: "short",
hour12:false,
timeZoneName :"short",
}
Configurable parameters. If they have the same properties as the default parameters, the passed in parameters will prevail
let options = {
era: "long", //Show ad:"narrow", "short", "long"
year: "numeric", //"numeric", "2-digit"
month: "numeric", //"numeric", "2-digit", "narrow", "short", "long"
day: "numeric", //"numeric", "2-digit"
hour: "numeric", //"numeric", "2-digit"
minute: "numeric",//"numeric", "2-digit"
second: "numeric",//"numeric", "2-digit"
weekday: "long", //"narrow", "short", "long"
hour12:false, // true | false
timeZoneName :"short", //"short", "long"
timeZone:"", // Specify the time zone for the city
}
Reference value of:options.timeZone
- For other values, please refer to:https://www.iana.org/time-zones
timeZone:'UTC' // UTC time
timeZone:'Europe/Berlin' // GMT+1; German time zone
timeZone:'Australia/Sydney' // GMT+11; Australian time zone
timeZone:'Europe/Moscow' // GMT+3; Russian time zone
timeZone:'Europe/Paris' // GMT+1; French time zone
timeZone:'Europe/London' // GMT; UK time zone
timeZone:'Asia/Tokyo' // JST; Tokyo time zone
timeZone:'Asia/Shanghai' // GMT+8; Beijing time zone
timeZone:'America/Denver' // MST; Us mountain time zone
timeZone:'America/Los_Angeles' // PTZ; Pacific time zone
timeZone:'America/New_York' // EST; Eastern time zone
timeZone:'America/Chicago' // CST; Central time zone
Version update history
v2.2.2
Update document parameter description
v2.2.1
Update document example reference
v2.2.0
The corresponding time can be specified for all API additions
v2.1.2
Verification of development and production environment
v2.1.1
add tsconfig.json
v2.1.0
Change the JS of the library to ts