|
| 1 | +--- |
| 2 | +sidebar_position: 1 |
| 3 | +title: Dates in Bitbybit |
| 4 | +sidebar_label: Dates in Bitbybit |
| 5 | +description: An overview of the Dates class in Bitbybit, explaining how to create, access, modify, and format date and time information. |
| 6 | +tags: [code, dates] |
| 7 | +--- |
| 8 | + |
| 9 | +<img |
| 10 | + src="https://s.bitbybit.dev/assets/icons/white/dates-icon.svg" |
| 11 | + alt="Dates category icon" |
| 12 | + title="Dates category icon" |
| 13 | + width="100" /> |
| 14 | + |
| 15 | +[View Full Source & Details on GitHub](https://github.com/bitbybit-dev/bitbybit/blob/master/packages/dev/base/lib/api/services/dates.ts) |
| 16 | + |
| 17 | +The `Dates` class in Bitbybit provides a collection of tools for working with dates and times. It largely leverages the built-in JavaScript `Date` object, offering convenient methods to create, manipulate, and query date information. |
| 18 | + |
| 19 | +**What is a Date in Bitbybit?** |
| 20 | + |
| 21 | +A "Date" in Bitbybit refers to a standard JavaScript `Date` object. This object encapsulates a specific moment in time, including the year, month, day, hour, minute, second, and millisecond. |
| 22 | + |
| 23 | +## Core Capabilities of the Dates Class |
| 24 | + |
| 25 | +The `Dates` class simplifies common date and time operations. Here's a high-level look at its features. For the exact input parameters (like DTOs) and detailed behavior (e.g., how months are 0-indexed), please consult the [full Dates API documentation](https://docs.bitbybit.dev/classes/Bit.Dates.html) or the GitHub source linked above. |
| 26 | + |
| 27 | +### 1. Creating Dates |
| 28 | + |
| 29 | +Need to define a specific date or get the current time? |
| 30 | +* **Current Date & Time (`now()`):** Get a `Date` object representing the exact moment the function is called. |
| 31 | +* **Specific Date (`createDate()`):** Create a `Date` object by providing individual components like year, month (0-11), day, hours, minutes, seconds, and milliseconds. |
| 32 | +* **Specific UTC Date (`createDateUTC()`):** Similar to `createDate()`, but interprets the provided components as Coordinated Universal Time (UTC). |
| 33 | +* **From Timestamp (`createFromUnixTimeStamp()`):** Create a `Date` object from a Unix timestamp (the number of milliseconds since January 1, 1970, UTC). |
| 34 | + |
| 35 | +### 2. Converting Dates to Strings (Formatting) |
| 36 | + |
| 37 | +Displaying dates in a human-readable format: |
| 38 | +* **Basic String (`toString()`):** Get a general string representation of the date (format depends on the system's locale). |
| 39 | +* **Date Part Only (`toDateString()`):** Get just the date portion as a string (e.g., "Mon Jan 01 2024"). |
| 40 | +* **Time Part Only (`toTimeString()`):** Get just the time portion as a string (e.g., "14:30:00 GMT+0000"). |
| 41 | +* **ISO Format (`toISOString()`):** Get the date in the standard ISO 8601 format (e.g., "2024-01-01T14:30:00.000Z"). This is often used for data interchange. |
| 42 | +* **JSON Format (`toJSON()`):** Get the date as a string suitable for JSON serialization (typically ISO format). |
| 43 | +* **UTC String (`toUTCString()`):** Get the date as a string formatted according to UTC conventions (e.g., "Mon, 01 Jan 2024 14:30:00 GMT"). |
| 44 | + |
| 45 | +### 3. Getting Specific Parts of a Date |
| 46 | + |
| 47 | +Extracting individual components from a `Date` object. Most "get" methods have both a local time version and a UTC version: |
| 48 | +* **Year:** `getYear()` / `getUTCYear()` |
| 49 | +* **Month:** `getMonth()` (0 for January, 11 for December) / `getUTCMonth()` |
| 50 | +* **Day of the Month:** `getDayOfMonth()` (1-31) / `getUTCDay()` (Note: `getUTCDay()` in JS Date returns day of week for UTC, but here it seems to mean day of month for UTC based on pairing with `setUTCDay` which takes day of month). |
| 51 | +* **Day of the Week:** `getWeekday()` (0 for Sunday, 6 for Saturday - local time) |
| 52 | +* **Hours:** `getHours()` (0-23) / `getUTCHours()` |
| 53 | +* **Minutes:** `getMinutes()` (0-59) / `getUTCMinutes()` |
| 54 | +* **Seconds:** `getSeconds()` (0-59) / `getUTCSeconds()` |
| 55 | +* **Milliseconds:** `getMilliseconds()` (0-999) / `getUTCMilliseconds()` |
| 56 | +* **Total Time (`getTime()`):** Get the raw numeric value of the date, represented as milliseconds since the Unix epoch (January 1, 1970, UTC). |
| 57 | + |
| 58 | +### 4. Setting Parts of a Date (Modifying Dates) |
| 59 | + |
| 60 | +Changing components of an existing `Date` object. These methods typically return a *new* `Date` object with the modification, leaving the original unchanged. Like "get" methods, "set" methods often have local and UTC versions: |
| 61 | +* **Year:** `setYear()` / `setUTCYear()` |
| 62 | +* **Month:** `setMonth()` / `setUTCMonth()` |
| 63 | +* **Day of the Month:** `setDayOfMonth()` / `setUTCDay()` |
| 64 | +* **Hours:** `setHours()` / `setUTCHours()` |
| 65 | +* **Minutes:** `setMinutes()` / `setUTCMinutes()` |
| 66 | +* **Seconds:** `setSeconds()` / `setUTCSeconds()` |
| 67 | +* **Milliseconds:** `setMilliseconds()` / `setUTCMilliseconds()` |
| 68 | +* **Total Time (`setTime()`):** Set the date based on a Unix timestamp (milliseconds since epoch). |
| 69 | + |
| 70 | +### 5. Parsing Date Strings |
| 71 | + |
| 72 | +* **Parse Date String (`parseDate()`):** Convert a string representation of a date into a Unix timestamp (number of milliseconds since epoch). The success of parsing depends on the format of the string and the browser's parsing capabilities. |
| 73 | + |
| 74 | +## Important Notes: |
| 75 | + |
| 76 | +* **Local Time vs. UTC:** Be mindful of whether you're working with local time (which depends on the user's system timezone) or UTC (a global time standard). The class provides methods for both. |
| 77 | +* **Month Indexing:** When creating dates or getting/setting months, remember that months are typically 0-indexed in JavaScript (0 for January, 1 for February, ..., 11 for December). |
| 78 | +* **Immutability:** The "set" methods in this class are designed to be immutable: they return a *new* `Date` object with the changes, rather than modifying the original `Date` object you passed in. This is generally a safer approach. |
0 commit comments