chronia

isSameMonth

Overview

The isSameMonth function checks whether two dates fall within the same calendar month and year. It provides a simple and reliable way to compare month equality in Chronia’s consistent API surface.

Signature

function isSameMonth(dateLeft: DateInput, dateRight: DateInput): boolean;

Parameters

Parameter Type Description
dateLeft DateInput The first date as a Date object, numeric timestamp, or ISO 8601 string
dateRight DateInput The second date as a Date object, numeric timestamp, or ISO 8601 string

Return Value

Type Description
boolean Returns true if both dates are in the same month and year, false otherwise or if either date is invalid

Description

The isSameMonth function determines whether two provided dates occur within the same calendar month and year. It ignores the day and time components, focusing solely on the month and year values. The function internally uses diffMonths to calculate the month difference and returns true when the difference is exactly zero.

Specification

Returns true when:

Returns false when:

Behavior Notes

Use Cases

Usage Examples

Event Grouping

import { isSameMonth } from "chronia";

interface Event {
  name: string;
  date: Date;
}

// Group events by month
function groupEventsByMonth(events: Event[], referenceDate: Date): Event[] {
  return events.filter((event) => isSameMonth(event.date, referenceDate));
}

const events: Event[] = [
  { name: "Meeting", date: new Date(2024, 5, 5) },
  { name: "Conference", date: new Date(2024, 5, 20) },
  { name: "Workshop", date: new Date(2024, 6, 10) },
];

const juneEvents = groupEventsByMonth(events, new Date(2024, 5, 1));
// Returns: [{ name: 'Meeting', date: ... }, { name: 'Conference', date: ... }]

// Same month, different days
isSameMonth(new Date(2024, 5, 1), new Date(2024, 5, 30)); // Returns: true

// Different months
isSameMonth(new Date(2024, 5, 30), new Date(2024, 6, 1)); // Returns: false

Date Range Validation

import { isSameMonth } from "chronia";

// Validate billing period
function isInSameBillingMonth(
  transactionDate: Date,
  billingDate: Date,
): boolean {
  return isSameMonth(transactionDate, billingDate);
}

const billingDate = new Date(2024, 5, 1);
const transaction1 = new Date(2024, 5, 15, 14, 30);
const transaction2 = new Date(2024, 6, 5);

isInSameBillingMonth(transaction1, billingDate); // Returns: true
isInSameBillingMonth(transaction2, billingDate); // Returns: false

// Same month and year, different times (time is ignored)
isSameMonth(new Date(2024, 5, 15, 14, 30), new Date(2024, 5, 20, 9, 45)); // Returns: true

Calendar Navigation

import { isSameMonth } from "chronia";

// Check if a date is in the currently displayed month
function isInCurrentView(date: Date, viewDate: Date): boolean {
  return isSameMonth(date, viewDate);
}

const currentView = new Date(2024, 5, 1); // June 2024
const selectedDate1 = new Date(2024, 5, 15);
const selectedDate2 = new Date(2024, 4, 15);

isInCurrentView(selectedDate1, currentView); // Returns: true
isInCurrentView(selectedDate2, currentView); // Returns: false

// Same month, different years (year matters)
isSameMonth(new Date(2024, 5, 15), new Date(2023, 5, 15)); // Returns: false

Data Filtering

import { isSameMonth } from "chronia";

interface LogEntry {
  message: string;
  timestamp: number;
}

// Filter logs from the same month
function getLogsForMonth(logs: LogEntry[], referenceDate: Date): LogEntry[] {
  return logs.filter((log) => isSameMonth(log.timestamp, referenceDate));
}

const logs: LogEntry[] = [
  { message: "Error", timestamp: new Date(2024, 5, 10).getTime() },
  { message: "Warning", timestamp: new Date(2024, 5, 25).getTime() },
  { message: "Info", timestamp: new Date(2024, 6, 5).getTime() },
];

const juneLogs = getLogsForMonth(logs, new Date(2024, 5, 1));
// Returns: [{ message: 'Error', ... }, { message: 'Warning', ... }]

// Works with numeric timestamps
isSameMonth(new Date(2024, 5, 15).getTime(), new Date(2024, 5, 20).getTime()); // Returns: true

Comparison Logic

import { isSameMonth } from "chronia";

// Check if subscription renewal is in the same month
function needsRenewalNotice(lastRenewal: Date, checkDate: Date): boolean {
  return !isSameMonth(lastRenewal, checkDate);
}

const lastRenewal = new Date(2024, 5, 1);
const today = new Date(2024, 6, 1);

needsRenewalNotice(lastRenewal, today); // Returns: true (different months)

// Invalid dates return false
isSameMonth(new Date("invalid"), new Date(2024, 5, 1)); // Returns: false
isSameMonth(NaN, new Date(2024, 5, 1)); // Returns: false
isSameMonth(Infinity, new Date(2024, 5, 1)); // Returns: false