React Date Object
supported calendars: gregorian
, persian
, arabic
default: gregorian
supported locals: en
, fa
, ar
default: en
NodeJs: date-object
Install
npm:
npm install react-date-object --save
yarn:
yarn add react-date-object
Usage
import DateObject from "react-date-object";
var dateObject = new DateObject();
Example
1- new instance
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");
1-2- Number (unix timestamp)
var date = new DateObject(1597994736);
date.format("dddd DD MMMM @ hh:mm:ss.SSS a");
1-3- JavaScript Date
var $date = new Date(2019, 8, 20);
var date = new DateObject($date);
date.format();
1-4- DateObject
var $date = new DateObject("2019/09/20");
var date = new DateObject($date);
date.format();
1-5- Object
1-5-1-
{
date: String , Number(unix timestamp), JavaScript Date or DateObject,
calendar: `gregorian`, `persian` or `arabic`,
local: `en`, `fa` or `ar`,
format: `String`
}
var date = new DateObject({
date: new Date(),
calendar: "gregorian",
local: "en",
});
date.format();
date = new DateObject({
date: new Date(),
calendar: "persian",
local: "fa",
});
date.format();
date = new DateObject({
date: "31 Mordad 1399",
calendar: "persian",
local: "en",
format: "DD MMMM YYYY",
});
date.format();
1-5-2-
{
year: Number,
month: Number,
day: Number,
hour: Number,
minute: Number,
second: Number,
millisecond: Number,
calendar: `gregorian`, `persian` or `arabic`,
local: `en`, `fa` or `ar`,
format: String
}
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",
local: "en",
format: "dddd DD MMMM YYYY",
});
date.format();
date = new DateObject({
year: 1399,
month: 5,
day: 31,
calendar: "persian",
local: "fa",
format: "dddd DD MMMM YYYY",
});
date.format();
2- setter methods
var date = new DateObject();
date
.setCalendar("gregorian")
.setFormat("YYYY-MM-DD HH:mm:ss.SSS")
.setLocal("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();
3- 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);
console.log(`${date.weekDay.name} ${date.year}/${date.month}/${date.day}`);
date.year = 2020;
date.month = 8;
date.day = 21;
date.isLeap;
date.isValid;
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.weeks;
date.months;
date.leaps;
4- parse method
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");
5- getProperty
let date = new DateObject({
calendar: "arabic",
date: "1442/01/01",
local: "ar",
});
date.getProperty("M");
date.format("M");
typeof date.getProperty("D");
typeof date.format("D");
Number(date.getProperty("YYYY"));
Number(date.format("YYYY"));
6- 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();
7- 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()}
`);
8- 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.valueOf();
date.toObject();
using calendars, format & locals
var date = new DateObject({ calendar: "gregorian", format: "dddd DD MMMM" });
date.format();
date.calendar = "persian";
date.local = "fa";
date._format = "YY/MM/DD";
date.setCalendar("gregorian").setLocal("en");
date = new DateObject(new Date());
date.convert("persian").format();
date.convert("arabic").format();
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 | week of year | 01, 03, 24, 33, ... | format |
W | week of year | 1, 3, 24. 33, ... | format |
dddd | week day name | Saturday, Sunday, Monday, ... | format |
ddd | week day short name | Sat, Sun, Mon, ... | 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 |