Lyra-JS

lr.convertDate()

convertDate is an important and useful function as most date functions in JavaScript and other date libraries, including this one, expects a valid date string or date object as an argument.

convertDate can convert any date format to a valid date object or string. It takes in three arguments, first one is the format of the date you are passing on, second is the date and third is whether you want a date string or object.

If you would like a date object, pass a string "object" as the third argument, like in the third example below.

lr.convertDate("dd, mm yy", "30, June 2018") // Returns Sat Jun 30 2018 00:00:00

lr.convertDate("yy dd-mm hh:min:ss", " 2018 28-3 13:30:00") // Returns Wed Mar 28 2018 13:30:00

lr.convertDate("yy-mm hh-min @dd th", "2018-07 15:10 @10 th", "object") // Returns date object
Date formatting keys:

yy (required) - Full year. Eg: 2018

mm (required) - Full Month name, half month name or month number. Eg: January, Sep or 7

dd (required) - Date number. Eg: 3

hh (optional*) - Hour number. Eg: 13, 3

min (optional) - Minute number. Eg: 25

ss (optional) - Second number. Eg: 5

*Hours is optional; however, it is required if min is mentioned in the format.

Note: Formatting keys are case sensitive and shouldn't have a letter or word attached to it. Eg: ddday for 15day is invalid.

lr.getDate()

Get a date in any format you like, essentially the opposite of lr.convertDate. It takes in two arguments, first the format you would like the date to be in and second which is optional, a specific date that needs to be a valid date string or object.

lr.getDate("{dd}th, {fm} {yy}", "Sat Jun 30 2018") // Returns 30th, June 2018

lr.getDate("{dd} {sm}") // Returns current date in specified format (Eg: 18 Sep)

lr.getDate("{12hh}:{min}:{ss} {ampm}") // Returns time in 12 hour format with meridiem in lower case (Eg: 11:30:06 pm)
Formatting keys:

{yy}- Full year. Eg: 2018

{mm}- Month number. Eg: 12

{fm}- Full month name. Eg: February

{sm}- Short month name. Eg: May

{dd}- Day number. Eg: 5

{dn}- Day name. Eg: Tuesday

{dsn}- Short day name. Eg: Fri

{hh}- 24 Hour number. Eg: 13

{12hh}- 12 Hour number. Eg: 1

{AMPM}- Meridiem in upper case. Eg: PM

{ampm}- Meridiem in lower case. Eg: pm

{min}- Minute number. Eg: 56

{ss}- Second number. Eg: 12

lr.getTimeByZone()

Get date and time of any time zone. The function takes one argument, which is the time zone ID of the time offset.

lr.getTimeByZone("JST") // Returns time in Japan (JST - Japanese Standard Time)

lr.getTimeByZone("IST") // Returns time in India (IST - Indian Standard Time)

Time offsets:

Code Offset
UTC (Universal Time Coordinated) +0:00
ECT (European Central Time) +1:00
EET, ART (Eastern European Time, (Arabic) Egypt Standard Time) +2:00
EAT (East Africa Time) +3:00
MET (Middle European Time) +3:30
NET (Near East Time) +4:00
PLT (Pakistan Lahore Time) +5:00
IST (Indian Standard Time) +5:30
BST (Bangladesh Standard Time) +6:00
VST (Vietnam Standard Time) +7:00
CTT (China Taiwan Time) +8:00
JST (Japan Standard Time) +9:00
ACT (Australia Central Time) +9:30
AET (Australia Eastern Time) +10:00
SST (Solomon Standard Time) +11:00
NST (New Zealand Standard Time) +12:00
MIT (Midway Islands Time) -11:00
HST (Hawaii Standard Time) -10:00
AST (Alaska Standard Time) -9:00
PST (Pacific Standard Time) -8:00
PNT, MST (Phoenix Standard Time, Mountain Standard Time) -7:00
CST (Central Standard Time) -6:00
EST, IET (Eastern Standard Time, Indiana Eastern Standard Time) -5:00
PRT (Puerto Rico and US Virgin Islands Time) -4:00
CNT (Canada Newfoundland Time) -5:00
AGT, BET (Argentina Standard Time, Brazil Eastern Time) -3:00
CAT (entral African Time) -1:00

If you know the time offset or want to know the time zone ID of any device use lr.getCC

lr.convertTimeByZone()

Convert time from one country's / time zone's to another country's / time zone's time. The function takes in three arguments, first one is the initial time zone ID, second is the time zone ID you want to convert to, third is the date and time that needs to be a valid date string or object.

lr.convertTimeByZone("IST","JST","Sat, 30 Jun 2018 12:43:00") // Returns Sat Jun 30 2018 16:13:00

lr.getCC()

Get date and time of any time zone. The function takes one argument, which is the time zone ID of the time offset.

lr.getCC() // Retruns time zone ID for device

lr.getCC(4) // Returns time zone ID for time offset of +4:00 hours

lr.getCC(-3.5) // Returns the time zone ID for the time offset of -3:30 hours

lr.add()

Adds specified parameters to date. The function takes two arguments, first is by how much you want to move the date forward to, second is the date, second argument is optional. If a second argument is not provided it will use current date.

lr.add("Tuesday", "Sat Jun 30 2018 16:13:00") // Moves date to next Tuesday. Returns Tue Jul 03 2018 16:13:00

lr.add("5 days") // Adds 5 days to current date

Keys:

Days- Eg: 5 Days or 1 Day

Months-Eg: 2 Months

Years- Eg: 4 Years

Name of day- Eg: Wednesday

Hours- Eg: 5 Hours

Minutes-Eg: 2 Minutes

Seconds- Eg: 4 Seconds

lr.sub()

Subtracts specified parameters to date. The function takes two arguments, first is by how much you want to move the date back to, second is the date, second argument is optional. If a second argument is not provided it will use current date.

lr.sub("Wed", "Sat Jun 30 2018 16:13:00") // Moves date back to Wednesday. Returns Wed Jun 27 2018 16:13:00

lr.sub("5 days") // Subtracts 5 days from current date

Keys:

Days- Eg: 5 Days or 1 Day

Months-Eg: 2 Months

Years- Eg: 4 Years

Name of day- Eg: Wednesday

Hours- Eg: 5 Hours

Minutes-Eg: 2 Minutes

Seconds- Eg: 4 Seconds

lr.dateDifference()

Finds difference between two dates (Leap Year Included). The functions takes three parameters, first the date you want in the L.H.S, second the date you want in the R.H.S, third argument is if you want time too.

Pass string "long" in the third parameter if you want hours, minutes and seconds

lr.dateDifference("Fri Sep 14 2018 15:31:10", "Wed Jun 27 2018 16:13:00", "long")
// Returns 2 Months 16 Days 23 Hours 18 Minutes 10 Seconds

lr.dateDifference("Fri Dec 5 2018", "Wed May 2 2010") // Returns 8 Years 7 Months 4 Days