Descriptions

The DateObject is an ES6 class that I wrote to make it easier to work with a variety of calendars, so there is no need to import a separate package into the DatePicker for each calendar.

This package can be used in the backend and frontend as well.

DateObject has several other options that make it much easier to work with date and time, and in this section, I want to write about some of the most useful options.

Current Moment

You need to enter 'new DateObject()' to generate an instance of DateObject. If you enter the above code without any constructor, you will get an object containing the current moment with the Gregorian calendar and English locale, for example for today, which is 03/02/2021, if I define a new DateObject(), An object of this moment will be produced for me:

const date = new DateObject()

console.log(date.year) //2021
console.log(date.month.number) //3
console.log(date.day) //2
console.log(date.calendar) //gregorian
console.log(date.locale) //en

Now you can format your date with the format method:

console.log(date.format()) //2021/03/02

Also, if you have a specific token, you can enter that token as a parameter in the format method:

console.log(date.format("dddd DD MMMM YYYY")) //Tuesday 02 March 2021

And if you use a value inside your tokens that you want to ignore, define those values as an array in the second parameter:

console.log(
  date.format(
    "Date: YYYY/MM/DD Time: HH:mm:ss",
    ["Date", "Time"]
  )
) //Date: 2021/03/02 Time: 10:53:00

As you know, you can destructure this object and get the values you need from it:

const {year, month, day, hour, minute} = date

In the same way, you can destructure the name, shortName, number, and index, from the value of the month & weekDay.

const {name, shortName, number, index} = date.month

console.log(name, shortName, number, index); //March Mar 3 2
const {name, shortName, number, index} = date.weekDay

console.log(name, shortName, number, index); //Tuesday Tue 3 2

If you want to move to the beginning or end of the month, use the toFirstOfMonth() and toLastOfMonth() methods.

date.toFirstOfMonth()

console.log(date.format()); //2021/03/01

date.toLastOfMonth()

console.log(date.format()); //2021/03/31

Adding / Subtracting Time From Original Moment

Depending on the increase or decrease of time from the original moment, add or subtract methods can be used.

So you have to pass two parameters of value and key to these methods.

The type of value can be a number or string, and the type of key must be a string.

All the keys that can be passed as the second parameter are listed in the table below.

Keys
yearsyeary
monthsmonthM
daysdayd
hourshourh
minutesminutem
secondsseconds
millisecondsmillisecondms

For example, to add 5 days to the present moment, you can do the following:

const date = new DateObject() //2021/03/02

date.add(5, "days");

console.log(date.format()); //2021/03/07

Subtracting 1 month from the above date

date.subtract(1, "month"); 

console.log(date.format()); //2021/02/07

You can also do all these operations continuously:

console.log(
  new DateObject()
    .add(5, "days")
    .subtract(1, "month")
    .format()
); //2021/02/07

Persian Calendar (Solar Hijri)

If you want to generate current moment in calendars other than Gregorian, just insert an object with a calendar property equal to the calendar you want, into the DateObject constructor:

const date = new DateObject({ calendar: "persian" })

console.log(date.format()) //1399/12/12

Arabic Calendar (islamic hijri)

const date = new DateObject({ calendar: "arabic" })

console.log(date.format()) //1442/07/18

Indian Calendar

const date = new DateObject({ calendar: "indian" })

console.log(date.format()) //1942/12/11

Table of most used properties

PropertyTypeExample
DateObjectJavascript Date
yeargetFullYear()Number2021
month-Object{ length: 31, name: 'March', shortName: 'Mar', index: 2, number: 3, toString: [Function (anonymous)], valueOf: [Function (anonymous)] }
month.indexgetMonth()Number2
daygetDate()Number2
weekDay-Object{ index: 2, number: 3, toString: [Function: toString], valueOf: [Function: valueOf], name: 'Tuesday', shortName: 'Tue' }
weekDay.indexgetDay()Number2
hourgetHours()Number10
minutegetMinutes()Number53
secondgetSeconds()Number24
millisecondgetMilliseconds()Number458
valueOf()valueOf()Number1614672704244
-getTimezoneOffset()Number-210
toUTC()getUTCDate()
setYear()setFullYear()setYear(2021)
setMonth()setMonth()setMonth(10)*
setDay()setDate()setDay(7)

*The setMonth method in JavaScript accepts the month index, but in DateObject you have to enter the month number.

You can also use the following methods to set the amount of time in DateObject:

const date = new DateObject()

date.year = 2020
date.month = 3
date.day = 4

console.log(date.format()) //2020/03/04

date.set({ year: 2010, month: 4, day: 7 })

console.log(date.format()) //2010/04/07

console.log(
  date
    .setYear(2000)
    .setMonth(1)
    .setDay(1)
    .format()
) //2000/01/01

console.log(
  date
    .set("year", 1907)
    .set("month", 7)
    .set("day", 7)
    .format()
) //1907/07/07

New DateObject From String

There are several ways to generate a date from a string:

  • You can insert that string directly into the DateObject constructor. For this purpose, you should pay attention to the following two items.

    1. If you enter the string directly, the date will be parsed as the Gregorian calendar.

    2. The string must be entered with the following format: year month day hour minute second millisecond meridiem and you can replace the spaces with any character you want.

    var date = new DateObject("2020 8 21 11 55 36 100 am");
    
    date.format("YYYY/MM/DD hh:mm:ss.SSS a"); //2020/08/21 11:55:36.100 am
    
    date = new DateObject("2020/08/01");
    
    date.format("YYYY/MM/DD hh:mm:ss.SSS a"); //2020/08/01 12:00:00.000 am
  • If you want to parse a string with a specific format, you must insert that string and format, to an object, and passed it to the DateObject constructor.

    const date = new DateObject({
      date: "September 04 2021, 12:42 am",
      format: "MMMM DD YYYY, HH:mm a"
    })
    
    console.log(date.format()); //September 04 2021, 12:42 pm

    If you want to parse your string on a calendar & locale other than Gregorian & English, you must add the value of that calendar & locale to your object.

    const date = new DateObject({
      date: "1400/05/14 18:35:44",
      format: "YYYY/MM/DD HH:mm:ss",
      calendar: "persian",
      locale: "fa"
    })
    
    console.log(
      date.format("DD MMMM سال YYYY, ساعت HH و mm دقیقه")
    ); //۱۴ مرداد سال ۱۴۰۰, ساعت ۱۸ و ۳۵ دقیقه

New DateObject from javascript Date

const date = new DateObject(new Date(2020, 1, 15))

console.log(date.format()) //2020/01/15

Persian DateObject from javascript Date

const date = new DateObject({
  date: new Date(2020, 1, 15),
  calendar: "persian"
})

console.log(date.format()) //1399/10/26

New DateObject From Numbers

const date = new DateObject({
  year:2021,
  month:10,
  day:22,
})

console.log(date.format("dddd DD MMMM YYYY")) //Friday 22 October 2021

New DateObject from Unix Timestamp

If you visit here, you can get the Unix timestamp of the current moment.

The Unix timestamp At the moment I am writing this, is 1614678083.

Just note that this value is in seconds and we have to multiply it by 1000 to convert it to milliseconds:

const date = new DateObject({
  date: 1614678083 * 1000,
  calendar: "gregorian"
})

console.log(date.format()) //2021/03/02

You can also enter the value for the Gregorian calendar in this way:

const date = new DateObject(1614678083 * 1000)

If you want to know the Unix value of each date, you must use the 'toUnix()' method. For example, for the above variable, if we use the toUnix() method, the same number will be printed for us again:

console.log(date.toUnix()) //1614678083

Persian Calendar to Arabic Calendar

Use the convert method to convert any calendar to other calendars.

const date = new DateObject({calendar:"persian", date:"1399/12/24"})
        
date.convert("arabic")

console.log(date.format()) //1442/07/30

Arabic Calendar to Persian Calendar

const date = new DateObject({ calendar: "arabic", date: "1442/05/10" })

date.convert("persian")

console.log(date.format()) //1399/10/05

Gregorian Calendar to Indian Calendar

console.log(new DateObject().convert("indian").format()) //1942/12/11