React Date Object
supported calendars: gregorian
, persian
, arabic
, indian
default: gregorian
supported locales: en
, fa
, ar
, hi
default: en
NodeJs: date-object
1- Install
npm:
npm install react-date-object --save
yarn:
yarn add react-date-object
2- Usage
import DateObject from "react-date-object";
var dateObject = new DateObject();
3- static properties
3-1- calendars
returns supported calendars
{
GREGORIAN: 'GREGORIAN',
PERSIAN: 'PERSIAN',
ARABIC: 'ARABIC',
INDIAN: 'INDIAN'
}
3-2- locales
returns supported locales
{
EN: 'EN',
FA: 'FA',
AR: 'AR',
HI: 'HI'
}
4- Examples
4-1- new instance
4-1-1- String (year month day hour minute second millisecond meridiem)
var date = new DateObject("2020 8 21 11 55 36 100 am");
date.format("YYYY/MM/DD hh:mm:ss.SSS a");
date = new DateObject("2020/08/01");
date.format("YYYY/MM/DD hh:mm:ss.SSS a");
4-1-2- Number (unix time in milliseconds)
var date = new DateObject(1597994736000);
date.format("dddd DD MMMM @ hh:mm:ss.SSS a");
4-1-3- JavaScript Date
var $date = new Date(2019, 8, 20);
var date = new DateObject($date);
date.format();
4-1-4- DateObject
var $date = new DateObject("2019/09/20");
var date = new DateObject($date);
date.format();
4-1-5- Object
4-1-5-1-
{
date: String , Number(unix time in milliseconds), JavaScript Date or DateObject,
calendar: `gregorian`, `persian`, `arabic` or `indian`,
locale: `en`, `fa`, `ar` or `hi`,
format: String,
ignoreList:Array
}
var date = new DateObject({
date: new Date(),
calendar: "gregorian",
locale: "en",
});
date.format();
date = new DateObject({
date: new Date(),
calendar: "persian",
locale: "fa",
});
date.format();
date = new DateObject({
date: "31 Mordad 1399",
calendar: "persian",
locale: "en",
format: "DD MMMM YYYY",
});
date.format();
4-1-5-2-
{
year: Number,
month: Number,
day: Number,
hour: Number,
minute: Number,
second: Number,
millisecond: Number,
calendar: `gregorian`, `persian`, `arabic` or `indian`,
locale: `en`, `fa`, `ar` or `hi`,
format: String,
ignoreList:Array
}
var date = new DateObject({
year: 2020,
month: 8,
day: 21,
hour: 12,
minute: 20,
second: 36,
milisecond: 152,
format: "dddd DD MMMM YYYY",
});
date.format();
date = new DateObject({
year: 1399,
month: 5,
day: 31,
calendar: "persian",
locale: "en",
format: "dddd DD MMMM YYYY",
});
date.format();
date = new DateObject({
year: 1399,
month: 5,
day: 31,
calendar: "persian",
locale: "fa",
format: "dddd DD MMMM YYYY",
});
date.format();
4-2- convert(calendar:String)
if you use the convert method without argument, the date will be converted to Gregorian calendar.
var date = new DateObject();
date.convert(DateObject.calendars.PERSIAN);
date.convert(DateObject.calendars.ARABIC);
date.convert(DateObject.calendars.INDIAN);
date.convert(DateObject.calendars.GREGORIAN);
4-3- format(token:String)
default format is YYYY/MM/DD.
to see all format types click here
var date = new DateObject();
date.format();
date.format("MM/DD/YYYY");
date.format("MMM/DD/YYYY HH:mm:ss");
date.format("dddd DD MMMM YYYY, hh:mm:ss A");
date.format("ddd DD MMM YYYY, hh:mm a");
formatting with ignore list:
var date = new DateObject();
date.format("hours:HH minutes:mm seconds:ss", ["hours", "minutes", "seconds"]);
date.format("it's HH o'clock", ["it's", "o'clock"]);
date.format(`DD MMMM at hh:mm ${date.hour >= 12 ? "Afternoon" : "Morning"}`, [
"at",
"Afternoon",
"Morning",
]);
4-4- setter methods
var date = new DateObject();
date
.setCalendar("gregorian")
.setFormat("YYYY-MM-DD HH:mm:ss.SSS")
.setLocale("en")
.setYear(2020)
.setMonth(8)
.setDay(21)
.setHour(12)
.setMinute(20)
.setSecond(14)
.setMillisecond(200);
date.format();
date
.setCalendar("persian")
.setYear(1399)
.setMonth(8)
.setDay(21)
.setFormat("dddd DD MMMM YYYY");
date.format();
date.setDate(new Date(2020, 9, 27)).format();
date
.setDate(new DateObject({ calendar: "indian", year: 1942, month: 8, day: 5 }))
.format();
4-5- get and set
var date = new DateObject();
date.format();
date.year += 2;
date.month += 2;
date.day += 2;
date.hour += 2;
date.minute += 2;
date.second += 2;
date.millisecond += 2;
date.format();
console.log(date.month);
console.log(date.weekDay);
var { year, month, weekDay, day } = date;
console.log(`${weekDay.name} ${year}/${month}/${day}`);
date.year = 2020;
date.month = 8;
date.day = 21;
date.isLeap;
date.isValid;
date.isUTC;
date.month.name;
date.month.length;
date.month.index;
date.month.number;
date.weekDay.name;
date.weekDay.index;
date.weekDay.number;
date.dayOfBeginning;
date.dayOfYear;
date.daysLeft;
date.weekOfYear;
date.unix;
date.weekDays;
date.months;
date.leaps;
4-6- parse(date:String)
Remember that parsing date is based on the format you set
var date = new DateObject();
date._format = "dddd DD MMMM YYYY";
date.parse("Monday 24 August 2020");
date.format("YYYY/MM/DD");
date.parse("Friday 07 August 2020");
date.format("YYYY-MM-DD");
date.setCalendar("persian").setFormat("YYYY/MM/DD").parse("1399/06/03");
date.format();
date.setFormat("YYYY/MM/DD HH:mm").parse("1399/06/03 12:32");
date.format("dddd DD MMMM @ hh:mm a");
4-7- getProperty
let date = new DateObject({
calendar: "arabic",
date: "1442/01/01",
locale: "ar",
});
date.getProperty("M");
date.format("M");
typeof date.getProperty("D");
typeof date.format("D");
Number(date.getProperty("YYYY"));
Number(date.format("YYYY"));
4-8- add(duration:Number or String,type:String)
Types
|
---|
years | year | y |
months | month | M |
days | day | d |
hours | hour | h |
minutes | minute | m |
seconds | second | s |
milliseconds | millisecond | ms |
var date = new DateObject("2020/10/07 5:35:24 pm");
date.setFormat("YYYY/MM/DD hh:mm:ss.SSS");
date.add(2, "years").format();
date.add("1", "month").format();
date.add(3, "d").format();
date.add(4, "hours").format();
date.add(1, "minute").format();
date.add("20", "s").format();
date.add(100, "milliseconds").format();
4-9- subtract(duration:Number or String,type:String)
Types
|
---|
years | year | y |
months | month | M |
days | day | d |
hours | hour | h |
minutes | minute | m |
seconds | second | s |
milliseconds | millisecond | ms |
var yesterday = new DateObject().subtract(1, "day");
4-10- set method
4-10-1 set(key:String,value:Any)
var date = new DateObject();
date.set("year", 2021);
date.set("month", 1);
date.set("day", 7);
date.set("format", "MM/DD/YYYY");
date.set("calendar", "indian");
date.set("locale", "hi");
date.set("date", new DateObject({ calendar: "persian", locale: "en" }));
date.set("date", new Date());
4-10-2 set(object)
var date = new DateObject();
date.set({ calendar: "persian", format: "dddd DD MMMM YYYY" });
date.set({ calendar: "gregorian", year: 2020, month: 11, day: 12 });
date.set(new DateObject().toObject());
date.set({ format: "Date:MM/DD/YYYY", ignoreList: ["Date"] }).format();
4-11- toUTC()
let date = new Date();
let dateObject = new DateObject({
date,
calendar: "arabic",
format: "ddd, DD MMM YYYY HH:mm:ss",
});
console.log(`
dateUTC : ${date.toUTCString()}
arabicUTC : ${dateObject.toUTC().toString()}
gregorianUTC : ${dateObject.convert().toString()}
`);
4-12- custom months, week days & digits
See the example below in case you want to use your personal data instead of default months, week days & digits
var date = new DateObject();
date.format("MMMM MMM");
date.format("dddd ddd");
date.format("D");
date.months = [
["jan", "j"],
["feb", "f"],
["mar", "m"],
["apr", "a"],
["may", "m"],
["jun", "j"],
["jul", "j"],
["aug", "a"],
["sep", "s"],
["oct", "o"],
["nov", "n"],
["dec", "d"],
];
date.weekDays = [
["su", "s"],
["mo", "m"],
["tu", "t"],
["we", "w"],
["th", "t"],
["fr", "f"],
["sa", "s"],
];
date.digits = ["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"];
date.format("MMMM MMM");
date.format("dddd ddd");
date.format("D");
console.log(date.month);
console.log(date.weekDay);
console.log(date.digits);
You can use date.setMonths(months).setWeekDays(weekDays).setDigits(digits)
as well
4-13- other methods
var date = new DateObject();
date.toFirstOfWeek();
date.toFirstOfMonth();
date.toFirstOfYear();
date.toFirstWeekOfYear();
date.toLastOfMonth();
date.toLastOfWeek();
date.toLastOfYear();
date.toLastWeekOfYear();
date.toString();
date.toDate();
date.toUnix();
date.toJulianDay();
date.toObject();
date.toJSON();
JSON.stringify(dateObject);
new DateObject(date.toJSON()).format();
4-14 valueOf()
returns unix time in milliseconds
var gregorian = new DateObject();
var persian = new DateObject({ date: gregorian, calendar: "persian" });
var arabic = new DateObject({ date: gregorian, calendar: "arabic" });
var indian = new DateObject({ date: gregorian, calendar: "indian" });
console.log(gregorian.valueOf(), gregorian.format());
console.log(persian.valueOf(), persian.format());
console.log(indian.valueOf(), indian.format());
console.log(arabic.valueOf(), arabic.format());
console.log(persian - gregorian === 0);
4-15- using calendars, format & locales
var date = new DateObject({ calendar: "gregorian", format: "dddd DD MMMM" });
date.format();
date.calendar = "persian";
date.locale = "fa";
date._format = "YY/MM/DD";
date.setCalendar("gregorian").setLocale("en");
date = new DateObject(new Date());
date.convert("persian").format();
date.convert("arabic").format();
5- format types
Type | Example | Description | Availability (Parse /Format) |
---|
YYYY | 2020 | full year | both |
YY | 20 | 2 digits year | both |
MMMM | December | month name | both |
MMM | Dec | month short name | both |
MM | 03, 09, 10, ... | 2 digits month number | both |
M | 3, 9, 10, ... | month number | both |
DDDD | 09 | day of year | format |
DDD | 9 | day of year | format |
DD | 03, 09, 10, 17, ... | 2 digits day of month | both |
D | 3, 9 ,10, 17 | day of month | both |
WW | 01, 03, 24, 33, ... | week of year | format |
W | 1, 3, 24. 33, ... | week of year | format |
dddd | Saturday, Sunday, Monday, ... | week day name | format |
ddd | Sat, Sun, Mon, ... | week day short name | format |
dd | 01,02,...,07 | 2 digits week day number | format |
d | 1,2,...,7 | week day number | format |
HH | 03, 09, 10, 17,... | 2 digits hour (24 hour mode) | both |
H | 3, 9, 10, 17,... | hour (24 hour mode) | both |
hh | 03, 09, 10, 17,... | 2 digits hour (12 hour mode) | both |
h | 3, 9, 10, 17,... | hour (12 hour mode) | both |
mm | 03, 09, 10, 17,... | 2 digits minute | both |
m | 3, 9, 10, 17,... | minute | both |
ss | 03, 09, 10, 17,... | 2 digits second | both |
s | 3, 9, 10, 17,... | second | both |
SSS | 100 | 3 digits millisecond | both |
SS | 10 | 2 digits millisecond | both |
S | 1 | 1 digit millisecond | both |
A | AM | meridiem | both |
a | am | meridiem lowercase | both |