Date Object
supported calendars: gregorian
, persian
, arabic
default: gregorian
supported locals: en
, fa
, ar
default: en
NodeJs: date-object
Install
npm install react-date-object --save
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` or `persian`,
local: `en` or `fa`,
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` or `persian`,
local: `en` or `fa`,
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- 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();
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();