3.3.0 • Published 1 month ago

date-fran v3.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

Date and time functionalities for JavaScript projects (TypeScript enabled).

Use the date-fran module like so:

**initialise whichever function you'd need.**
import { 
    tomorrowsDate,
    yesterdaysDate, 
    givenDateAndCurrentTime,
    dateAcronym,
    todaysDate,
    differenceInDays,
    dateForwardBuild,
    dateConstrainer,
    sameDateComparator,
    varyingDatesComparator,
    tomorrowsDateByGivenDate, 
    yesterdaysDateByGivenDate,
    yesterdaysFormDate,
    tomorrowsFormDate,
    todaysFormDate
} from "date-fran";

This project was borne out of the need or necessity of eliminating discrepancies with time data on mongo-database. You must have observed that saving the normal "new Date()" value on mongo-database, would return date data inconsistent with what is actually expected.

Hence, with this module, we can put-off those inaccuracies and enhance manuevrability with time data, by making comparisons, getting future date values, all of which are consistent with the classical operations expected on a JavaScript date data.

Examples:

The tomorrowsDate function, gives tomorrows date

The yesterdaysDate function, gives yesterdays date

The givenDateAndCurrentTime function gives todays date and current time, when the optional parametre isn't provided. If provided (in the form: YYYY-MM-DD or YYYY/MM/DD or as JS's date object), it returns the appropriate date-data (date and time) representation of the provided string-date.

The sameDateComparator function checks if both given dates supplied, are equal and returns true if they are; otherwise, false. Arguments can be of the form "YYYY-MM-DD", "YYYY/MM/DD" or "Sun Sep 11 2022" (when you apply JS's toDateString() attribute to a date variable.).

Whenever possible, always use the sameDateComparator like so:

const datesAreSame = sameDateComparator(firstDate.toDateString(), secondDate.toDateString())

The varyingDatesComparator function checks if the second(future date) being supplied, is greater than the first(previous date) and returns true if they are; otherwise, false.

A more complex usage follows thus:

1. Get all hotel reservations for yesterday:

const yesterdaysReservations = hotelReservations.filter(reservations => reservations.dateCreated.toLocaleDateString() === yesterdaysDate().toLocaleDateString());

2. Get all hotel arrivals for tomorrow:

const tomorrowsArrival = hotelReservations.filter(reservations => reservations.dateCreated.toLocaleDateString() === tomorrowsDate().toLocaleDateString());

3. if an individuals arrival date as gotten from an html-form is stated as: 2022-09-04 or 2022/09/04 N.B: format remains year -> month -> day as JS standard

const requiredDate = givenDateAndCurrentTime("2022-09-04") 
returned date ====> 2022-09-04T07:12:46.000Z

for database models: 
await Users.create({
    ...,
    dateCreated: givenDateAndCurrentTime() 
})
// This surpreses mongodb's use of the ususal "00:00:00" time and discrepancies of date, 
if the creation was made at 12 am and rather use the correct date and time

4. The dateForwardBuild and differenceInDays function are useful as follows;

const arrivalDate = givenDateAndCurrentTime("2022-08-24")
const departureDate = givenDateAndCurrentTime("2022-09-14")
const stayPeriodOfGuest = differenceInDays(arrivalDate, departureDate)
let count = 1;
while(count <= stayPeriodOfGuest){
    const billingDate = dateForwardBuild(arrivalDate.getFullYear(), arrivalDate.getMonth(), count);
    const billDetails = roomBilling(billingDate) // a helper function for your application
    count++
}
* A simple way of understanding the function is by saying; give me the date of n days from now.

5. The dateConstrainer function is applicable to html forms as below. If you want the maximum and minimum choosable date on a form to be 2022-08-30 and 2022-08-24 respectively, (in React, for instance), you'd say;

const permissibleMinWelcomeDate = givenDateAndCurrentTime("2022-08-24")
const permissibleMaxWelcomeDate = givenDateAndCurrentTime("2022-08-30")

const maxDate = dateConstrainer(permissibleMaxWelcomeDate as Date);
const minDate = dateConstrainer(permissibleMinWelcomeDate as Date);

useEffect(() => {
    (document as any).querySelector(".arrival-date").max = maxDate;
    (document as any).querySelector(".arrival-date").min = minDate;
}, []);

It can as well be used to constrain date-data fields on MongoDB, to accept dates which fall within the
permissible defined max and min dates. 

6 The yesterdaysFormDate tomorrowsFormDate and todaysFormDate functions, are specifically compatable for forms; and can be used thus:

ReactJS:
useEffect(() => {
    (document as any).querySelector(".arrival-date").min = yesterdaysFormDate;
    (document as any).querySelector(".arrival-date").max = todaysFormDate();
}, []);

Vanilla JS:
(document as any).querySelector(".arrival-date").max = todaysFormDate();
(document as any).querySelector(".arrival-date").min = yesterdaysFormDate;

OR

ReactJS:
useEffect(() => {
    (document as any).querySelector(".arrival-date").max = tomorrowsFormDate();
    (document as any).querySelector(".arrival-date").min = yesterdaysFormDate();
}, []);

Vanilla JS:
(document as any).querySelector(".arrival-date").max = tomorrowsFormDate();
(document as any).querySelector(".arrival-date").min = yesterdaysFormDate();

7. You could use the sameDateComparator function, to check if a routine has been performed already; in order to allow or constrain, if the routine hasn't been performed.

e.g if a routine exists that should bill guests a certain amount daily, you could say; const guestHasBeenBilledForToday = sameDateComparator(guestBill.dateCreated.toDateString(), todaysDate().toDateString()) if(!guestHasBeenBilledForToday){ billguestForToday() // your routine }

8. The dateAcronym function is used like so:

const jsDate = new Date()
const preferredFormat = `${givenDateAndCurrentTime().getDate()}`.concat(dateAcronym(todaysDate().getDate())," ", monthAndIndex[jsDate.getMonth()], " ", `${jsDate.getFullYear()}`) 
// gives the date in the format: 11th Oct 2022 (for instance, with the appropriate acronym)

9. The tomorrowsDateByGivenDate function is used thus; say you'd like to calculate taxes payable, for the next thirty days and as well, append the dates for those days, you'd do thus:

let dayCount = 1;
const maxDaycount = 30;
let currentDate = yesterdaysDate() // if we start with yesterdays date, the first record date will be todays date
while(dayCount <= maxDayCount){
    const recordDate = tomorrowsDateByGivenDate(currentDate);
    const recordPayment = payTax({
        recordDate,
        index: dayCount,
        taxRate,
        .....
    });
    currentDate = recordDate;
    dayCount++
}

// if currentDate is say: 2022-05-17, 
// record date, when consoled, would give: 2022-05-18, 2022-05-19, 2022-05-20, ... to the 30th date 

10. The yesterdaysDateByGivenDate function is used thus; say you'd like to subtract an overcharge for purchases in the past twenty days and record this with the reference date, you'd do thus:

let dayCount = 1;
const maxDaycount = 20;
let currentDate = givenDateAndCurrentTime() // if we start with todays date, the first record date will be yesterdays date
while(dayCount <= maxDayCount){
    const recordDate = yesterdaysDateByGivenDate(`${currentDate.getFullYear()}-${currentDate.getMonth()+1}-${currentDate.getDate()}`);
    const recordPayment = deductOverCharge({
        recordDate,
        index: dayCount,
        .....
    });
    currentDate = recordDate;
    dayCount++
};

// if currentDate is say: 2022-05-17, 
// record date, when consoled, would give: 2022-05-16, 2022-05-15, 2022-05-14, ... to the 20th date 

11 The getMonth function: Get the actual (non-JS) month of a given date format

const productionMonth = getMonth(product.dateCreated)

12 The dateCountBuilder function: allows you to build past or future dates, according to the number of dates required. The function returns an array of dates in string or date type.

const stringDateForPastTenDays = dateCountBuilder(10, "down", "string")
// returns an array of string dates (for past ten days), begining from the presernt date
3.2.0

1 month ago

3.3.0

1 month ago

3.1.0

2 months ago

3.0.98

3 months ago

3.0.99

3 months ago

3.0.96

3 months ago

3.0.97

3 months ago

3.0.95

3 months ago

3.0.93

4 months ago

3.0.94

4 months ago

3.0.92

4 months ago

3.0.91

4 months ago

3.0.89

4 months ago

3.0.88

4 months ago

3.0.90

4 months ago

3.0.87

4 months ago

3.0.85

5 months ago

3.0.86

4 months ago

3.0.81

9 months ago

3.0.82

9 months ago

3.0.80

9 months ago

3.0.83

9 months ago

3.0.84

9 months ago

3.0.17

9 months ago

3.0.67

9 months ago

3.0.68

9 months ago

3.0.65

9 months ago

3.0.66

9 months ago

3.0.69

9 months ago

3.0.60

9 months ago

3.0.63

9 months ago

3.0.64

9 months ago

3.0.61

9 months ago

3.0.62

9 months ago

3.0.78

9 months ago

3.0.79

9 months ago

3.0.76

9 months ago

3.0.77

9 months ago

3.0.70

9 months ago

3.0.71

9 months ago

3.0.74

9 months ago

3.0.75

9 months ago

3.0.72

9 months ago

3.0.73

9 months ago

3.0.45

9 months ago

3.0.46

9 months ago

3.0.43

9 months ago

3.0.44

9 months ago

3.0.49

9 months ago

3.0.47

9 months ago

3.0.48

9 months ago

3.0.41

9 months ago

3.0.42

9 months ago

3.0.40

9 months ago

3.0.56

9 months ago

3.0.57

9 months ago

3.0.54

9 months ago

3.0.55

9 months ago

3.0.58

9 months ago

3.0.59

9 months ago

3.0.52

9 months ago

3.0.53

9 months ago

3.0.50

9 months ago

3.0.51

9 months ago

3.0.23

9 months ago

3.0.24

9 months ago

3.0.21

9 months ago

3.0.22

9 months ago

3.0.27

9 months ago

3.0.28

9 months ago

3.0.25

9 months ago

3.0.26

9 months ago

3.0.20

9 months ago

3.0.18

9 months ago

3.0.19

9 months ago

3.0.34

9 months ago

3.0.35

9 months ago

3.0.32

9 months ago

3.0.33

9 months ago

3.0.38

9 months ago

3.0.39

9 months ago

3.0.36

9 months ago

3.0.37

9 months ago

3.0.30

9 months ago

3.0.31

9 months ago

3.0.29

9 months ago

3.0.16

10 months ago

3.0.13

12 months ago

3.0.14

12 months ago

3.0.15

11 months ago

3.0.12

1 year ago

3.0.10

1 year ago

3.0.11

1 year ago

3.0.8

1 year ago

3.0.7

1 year ago

3.0.9

1 year ago

2.0.28

2 years ago

2.0.3

2 years ago

2.0.2

2 years ago

2.0.4

2 years ago

2.0.7

2 years ago

2.0.6

2 years ago

2.0.9

2 years ago

2.0.8

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

3.0.4

1 year ago

3.0.3

1 year ago

3.0.2

1 year ago

3.0.1

1 year ago

3.0.6

1 year ago

3.0.5

1 year ago

3.0.0

1 year ago

2.0.15

2 years ago

2.0.16

2 years ago

2.0.13

2 years ago

2.0.14

2 years ago

2.0.12

2 years ago

2.0.10

2 years ago

2.0.19

2 years ago

2.0.17

2 years ago

2.0.18

2 years ago

2.0.26

2 years ago

2.0.27

2 years ago

2.0.24

2 years ago

2.0.25

2 years ago

2.0.22

2 years ago

2.0.23

2 years ago

2.0.20

2 years ago

2.0.21

2 years ago

1.0.39

2 years ago

1.0.19

2 years ago

1.0.18

2 years ago

1.0.17

2 years ago

1.0.38

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.6

2 years ago

1.0.4

2 years ago

1.0.22

2 years ago

1.0.21

2 years ago

1.0.20

2 years ago

1.0.26

2 years ago

1.0.25

2 years ago

1.0.29

2 years ago

1.0.28

2 years ago

1.0.27

2 years ago

1.0.33

2 years ago

1.0.32

2 years ago

1.0.10

2 years ago

1.0.31

2 years ago

1.0.30

2 years ago

1.0.37

2 years ago

1.0.15

2 years ago

1.0.36

2 years ago

1.0.14

2 years ago

1.0.35

2 years ago

1.0.13

2 years ago

1.0.34

2 years ago

1.0.12

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago