lahze v0.3.7
Lahze
Lightweight package for formatting dates between jalali and gregorian. Supports both node and browser environments.
Usage
Basic usage
lahze can be initialized using 3 arguments: date, format of the date and locale.
After that lahze can manipulate and display date between locales easily.
import lahze from 'lahze';
lahze('1398/10/10', 'YYYY/MM/DD', 'fa').format('YYYY/MM/DD', 'en'); // 2019/12/31Date can be on of the following items:
- Instance of
Dateobject - String with a specific format
- Timestamp
If date is not specified or null, it will be equal to today.
- Instance of
Format accepted types are as followed:
YYYY: full year (4 digits).YY: short year (last 2 digits).MM: full month (2 digits).M: short month (1-2 digit).DD: full day (2 digits).D: short day (1-2 digits).HH: full hours (2 digits).H: short hours (1-2 digits).mm: full minutes (2 digits).m: short minutes (1-2 digits).ss: full seconds (2 digits).s: short seconds (1-2 digits).- Supported delimiters are
\,/,-,:.
If format is not specified or null, it will be equal to whatever
Dateobject accepts.Locale can be either fa or en (defaults en).
After initializing lahze with the desired arguments, you can easily manupulate date and display it with a specific format at the end.
Manipulating
Setters
setFullYear
This method accepts 4-digits year as the first argument and locale as it's second argument which specifies the locale of the first argument (defaults to initial locale which was specified by creating lahze instance).
lahze('1398/10/10', 'YYYY/MM/DD', 'fa').setFullYear(1399, 'fa'); // 1399/10/10setMonth
This method accepts month (value between 1 and 12) as the first argument and locale as it's second argument which specifies the locale of the first argument (defaults to initial locale which was specified by creating lahze instance).
lahze('1398/10/10', 'YYYY/MM/DD', 'fa').setMonth(12, 'fa'); // 1398/12/10setDay
This method accepts day of the month (value between 1 and 31) as the first argument and locale as it's second argument which specifies the locale of the first argument (defaults to initial locale which was specified by creating lahze instance).
lahze('1398/10/10', 'YYYY/MM/DD', 'fa').setDay(5, 'fa'); // 1398/10/5setHours
This method accepts hours (value between 0 and 23) as the first argument.
lahze('1398/10/10 12:00:00', 'YYYY/MM/DD HH:mm:ss', 'fa').setHours(5); // 1398/10/10 5:00:00setMinutes
This method accepts minutes (value between 0 and 59) as the first argument.
lahze('1398/10/10 12:00:00', 'YYYY/MM/DD HH:mm:ss', 'fa').setHours(50); // 1398/10/10 12:50:00setSeconds
This method accepts minutes (value between 0 and 59) as the first argument.
lahze('1398/10/10 12:00:00', 'YYYY/MM/DD HH:mm:ss', 'fa').setHours(10); // 1398/10/10 12:00:10Getters
getFullYear
This method returns full year (4-digits) of the lahze instance.
This method accepts locale as the first argument which specifies the locale of the returning value (defaults to initial locale which was specified by creating lahze instance).
lahze('1398/10/10', 'YYYY/MM/DD', 'fa').getFullYear('en'); // 2019
lahze('1398/10/10', 'YYYY/MM/DD', 'fa').getFullYear('fa'); // 1398getMonth
This method returns month number (1-12) of the lahze instance.
This method accepts locale as the first argument which specifies the locale of the returning value (defaults to initial locale which was specified by creating lahze instance).
lahze('1398/10/10', 'YYYY/MM/DD', 'fa').getMonth('fa'); // 10
lahze('1398/10/10', 'YYYY/MM/DD', 'fa').getMonth('en'); // 12getDay
This method returns day of the month of the lahze instance.
This method accepts locale as the first argument which specifies the locale of the returning value (defaults to initial locale which was specified by creating lahze instance).
lahze('1398/10/10', 'YYYY/MM/DD', 'fa').getDay('fa'); // 10
lahze('1398/10/10', 'YYYY/MM/DD', 'fa').getDay('en'); // 31getHours
This method returns hours of the month of the lahze instance.
lahze('1398/10/10 12:00:00', 'YYYY/MM/DD HH:mm:ss', 'fa').getHours(); // 12getMinutes
This method returns hours of the month of the lahze instance.
lahze('1398/10/10 12:20:00', 'YYYY/MM/DD HH:mm:ss', 'fa').getMinutes(); // 20getSeconds
This method returns hours of the month of the lahze instance.
lahze('1398/10/10 12:00:50', 'YYYY/MM/DD HH:mm:ss', 'fa').getSeconds(); // 50Advanced manipulation
You can set the year, month or day in a locale and get it in the opposite locale:
lahze('1398/10/10', 'YYYY/MM/DD', 'fa').setMonth(11, 'en').getMonth('fa'); // 8Display
lahze can return date in the given format with the desired locale:
lahze('1398/10/10', 'YYYY/MM/DD', 'fa').format('YYYY/MM/DD', 'en'); // 2019/12/31Supported formats are listed above.
License
MIT