Skip to content

[Edit] JavaScript:date: .setTime() #7277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 117 additions & 11 deletions content/javascript/concepts/dates/terms/setTime/setTime.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,145 @@
---
Title: '.setTime()'
Description: 'Sets the date and time by adding or subtracting a specified number of milliseconds to/from midnight January 1, 1970.'
Description: 'Sets the time value of a Date object using milliseconds since January 1, 1970 UTC'
Subjects:
- 'Computer Science'
- 'Web Development'
Tags:
- 'Date'
- 'JavaScript'
- 'Methods'
- 'Time'
CatalogContent:
- 'paths/web-development'
- 'introduction-to-javascript'
- 'paths/front-end-engineer-career-path'
---

The **`.setTime()`** method sets the date and time by adding or subtracting a specified number of milliseconds to/from midnight January 1, 1970.
The **`.setTime()`** method is a built-in JavaScript Date method that sets the time value of a `date` object using milliseconds since the Unix epoch (January 1, 1970, 00:00:00 UTC). This method allows developers to programmatically modify the date and time represented by a Date object by specifying the exact number of milliseconds from the reference point.

The `.setTime()` method is particularly useful when working with timestamps, converting between different time representations, or when there is a need to set a specific date and time based on millisecond values. It's commonly used in applications that handle time calculations, data processing, event scheduling, and any scenario where precise time manipulation is required.

## Syntax

```pseudo
myDate.setTime(millisec)
date.setTime(milliseconds)
```

The returned value from `.setTime()` is a value representing the number of milliseconds since the 1st of January 1970.
**Parameters:**

- `milliseconds`: A number representing the time value in milliseconds since January 1, 1970, 00:00:00 UTC. This can be a positive or negative integer.

**Return value:**

## Example
The `.setTime()` method returns the new timestamp value (in milliseconds) after setting the time. The method modifies the original Date object in place and returns the same millisecond value that was passed as the parameter.

In the example below, the `myDate` is declared and then the `.setTime()` method is applied to return a modified time.
## Example 1: Basic Usage of `.setTime()`

This example demonstrates the fundamental usage of `.setTime()` by setting a Date object to a specific timestamp:

```js
// Create a new Date object
const myDate = new Date();
myDate.setTime(1623918645000);

console.log(myDate.toString());
// Set the time to January 1, 2023, 12:00:00 UTC (1672574400000 milliseconds)
const timestamp = myDate.setTime(1672574400000);

// Display the results
console.log('Updated date:', myDate);
console.log('Returned timestamp:', timestamp);
```

This results in the following output:
This example results in the following output:

```shell
Sun Jun 17 2021 12:30:45 GMT+0000 (Coordinated Universal Time)
Updated date: 2023-01-01T12:00:00.000Z
Returned timestamp: 1672574400000
```

The Date object `myDate` is updated to represent January 1, 2023, at 12:00:00 UTC, and the method returns the timestamp value that was set.

## Example 2: Converting Between Time Formats

This example shows how `.setTime()` can be used to convert between different time representations and create standardized Date objects:

```js
// Create multiple Date objects for different time zones
const utcDate = new Date();
const localDate = new Date();

// Get current time in milliseconds
const currentTime = Date.now();

// Set both dates to the same timestamp
utcDate.setTime(currentTime);
localDate.setTime(currentTime);

// Add 24 hours (86400000 milliseconds) to create tomorrow's date
const tomorrowTime = currentTime + 86400000;
const tomorrowDate = new Date();
tomorrowDate.setTime(tomorrowTime);

console.log('Current time:', utcDate.toISOString());
console.log('Tomorrow time:', tomorrowDate.toISOString());
console.log(
'Time difference (hours):',
(tomorrowTime - currentTime) / (1000 * 60 * 60)
);
```

This example results in the following output:

```shell
Current time: 2025-07-07T12:01:46.940Z
Tomorrow time: 2025-07-08T12:01:46.940Z
Time difference (hours): 24
```

This demonstrates how `.setTime()` enables precise time calculations and conversions between different time representations.

## Codebyte Example: Database Timestamp Processing

This example illustrates a real-world scenario where `.setTime()` is used to process timestamps from a database and convert them to JavaScript Date objects:

```codebyte/javascript
// Simulated database records with Unix timestamps
const databaseRecords = [
{ id: 1, event: 'User Registration', timestamp: 1672574400000 },
{ id: 2, event: 'Purchase Made', timestamp: 1672660800000 },
{ id: 3, event: 'Account Updated', timestamp: 1672747200000 }
];

// Process records and convert timestamps to Date objects
const processedRecords = databaseRecords.map(record => {
const eventDate = new Date();
eventDate.setTime(record.timestamp);

return {
id: record.id,
event: record.event,
date: eventDate.toLocaleDateString(),
time: eventDate.toLocaleTimeString(),
fullDate: eventDate
};
});

// Display processed records
processedRecords.forEach(record => {
console.log(`Event ${record.id}: ${record.event} on ${record.date} at ${record.time}`);
});
```

This pattern is commonly used in web applications when processing timestamp data from databases, APIs, or log files, where the `.setTime()` method provides a reliable way to create Date objects from millisecond values.

## Frequently Asked Questions

### 1. What happens if I pass a negative number to `.setTime()`?

The method accepts negative numbers, which represent dates before January 1, 1970. For example, `-86400000` represents December 31, 1969.

### 2. Does `.setTime()` modify the original Date object?

Yes, `.setTime()` modifies the Date object in place. If you need to preserve the original date, create a copy first using `new Date(originalDate)`.

### 3. What's the difference between `.setTime()` and setting individual date components?

`.setTime()` sets the entire date and time in one operation using milliseconds, while methods like `.setFullYear()` or `.setMonth()` modify specific components. `.setTime()` is more efficient for setting complete timestamps.