Related Topics
Introduction
Html page 1
Html page 2
Html page3
Html page4
HTML Elements and structure
Html page 5
Html page 6
Html page 7
HTML Headings and Paragraphs
Html page 8
Html page 9
Html page 10
HTML Lists and Tables
Html page 11
Html page 12
Html page 13
HTML Forms and Input Fields
Html page 14
Html page 15
Html page 16
HTML Images and Media
Html page 17
Html page 18
HTML Links and Anchors
Html page 19
Html page 20
Html page 21
HTML Styles and Formatting
Html page 22
HTML Semantic Elements
Html page 23
Html page 24
HTML Attributes
Html page 25
Html page 26
HTML JavaScript Integration
Html page 27
Html page 28
Html page 29
Html page 30
HTML Document and Browser Support
Html page 31
Html page 32
HTML5 New Elements and Attributes
Html page 33
Html page 34
Html page 35
Html page 36
HTML Accessibility and Web Standards
Html page 37
Html page 38
Html page 39
HTML Responsive Design and Mobile Devices.
Html page 40
Html page 41
Html page 42
Introduction
Data Structure Page 1
Data Structure Page 2
Data Structure Page 3
Data Structure Page 4
Data Structure Page 5
Data Structure Page 6
Data Structure Page 7
Data Structure Page 8
String
Data Structure Page 9
Data Structure Page 10
Data Structure Page 11
Data Structure Page 12
Data Structure Page 13
Array
Data Structure Page 14
Data Structure Page 15
Data Structure Page 16
Data Structure Page 17
Data Structure Page 18
Linked List
Data Structure Page 19
Data Structure Page 20
Stack
Data Structure Page 21
Data Structure Page 22
Queue
Data Structure Page 23
Data Structure Page 24
Tree
Data Structure Page 25
Data Structure Page 26
Binary Tree
Data Structure Page 27
Data Structure Page 28
Heap
Data Structure Page 29
Data Structure Page 30
Graph
Data Structure Page 31
Data Structure Page 32
Searching Sorting
Data Structure Page 33
Hashing Collision
Data Structure Page 35
Data Structure Page 36

JAVASCRIPT
setTimeout(() => {
console.log('Delayed message!');
}, 2000); // 2000 milliseconds = 2 seconds
In this case, the callback function will be executed once after a delay of 2 seconds.
setInterval()
: ThesetInterval()
function is used to execute a callback function repeatedly at a specified interval. It also takes a callback function as the first parameter and the interval duration in milliseconds as the second parameter.
Here’s an example that displays a message every 1 second:
setInterval(() => {
console.log('Repeating message!');
}, 1000); // 1000 milliseconds = 1 second
In this case, the callback function will be executed every 1 second indefinitely until you manually stop it.
Both setTimeout()
and setInterval()
are useful for implementing time-based behavior, animations, periodically fetching data, and executing tasks after a specific delay. They are commonly used in web development to create timers, animations, and schedule tasks asynchronously.
It’s important to note that the timing of callbacks in setTimeout()
and setInterval()
functions is not guaranteed to be precise due to JavaScript’s single-threaded nature. The actual execution time may be delayed if the JavaScript thread is busy with other tasks.
setTimeout(() => {
console.log('Delayed message!');
}, 2000); // Execute after 2 seconds
In this case, the callback function is executed once after a delay of 2 seconds.
setInterval()
: ThesetInterval()
function is used to repeatedly execute a callback function at a specified interval. It starts executing the callback function immediately, and then continues to execute it at the specified interval until it is manually stopped or the browser tab/window is closed.
Here’s an example:
setInterval(() => {
console.log('Repeating message!');
}, 1000); // Repeat every 1 second
In this case, the callback function is executed immediately and then repeated every 1 second indefinitely until you manually stop it.
The key difference is that setTimeout()
executes the callback function only once after a specified delay, while setInterval()
executes the callback function repeatedly at a specified interval until stopped.
When using setInterval()
, it’s important to note that the intervals between executions are not guaranteed to be precise. The actual execution time may vary due to the single-threaded nature of JavaScript and the presence of other tasks in the event loop. Additionally, using setInterval()
indefinitely without stopping it can consume resources unnecessarily, so it’s crucial to clear the interval when it’s no longer needed using clearInterval()
.
Overall, setTimeout()
is ideal when you need to delay the execution of a function by a certain time, while setInterval()
is suitable when you need to repeatedly execute a function at a fixed interval.
// Set the target date and time for the countdown
const targetDate = new Date('2023-12-31T23:59:59');
// Update the countdown every second
const countdown = setInterval(() => {
// Get the current date and time
const currentDate = new Date();
// Calculate the remaining time in milliseconds
const remainingTime = targetDate - currentDate;
// Check if the countdown has reached zero or ended
if (remainingTime <= 0) {
clearInterval(countdown);
console.log('Countdown ended!');
return;
}
// Convert the remaining time to days, hours, minutes, and seconds
const days = Math.floor(remainingTime / (1000 * 60 * 60 * 24));
const hours = Math.floor((remainingTime % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const minutes = Math.floor((remainingTime % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((remainingTime % (1000 * 60)) / 1000);
// Display the remaining time
console.log(`Countdown: ${days}d ${hours}h ${minutes}m ${seconds}s`);
}, 1000); // Update every second
In this example, we set the targetDate
as the desired end date and time for the countdown. The setInterval()
function is used to update the countdown every second. Inside the callback function, we calculate the remaining time by subtracting the current date from the target date.
If the remaining time is zero or negative, we clear the interval and display a message indicating that the countdown has ended. Otherwise, we convert the remaining time to days, hours, minutes, and seconds using mathematical calculations. Finally, we display the remaining time in the console.
You can adjust the target date and customize the display of the remaining time as per your requirements.
const utcDate = new Date(Date.UTC(2023, 6, 14, 12, 30, 0));
console.log(utcDate.toUTCString());
Date.prototype.toUTCString()
method: ThetoUTCString()
method converts aDate
object to a string representation of the date and time in UTC format. This method returns a human-readable UTC string representation.
Example:
const currentDate = new Date();
console.log(currentDate.toUTCString());
Date.prototype.getUTC*()
methods: TheDate
object provides a set of methods with the prefixgetUTC*()
to retrieve specific date and time components in UTC format. For example,getUTCFullYear()
returns the four-digit year in UTC,getUTCMonth()
returns the month (0-11) in UTC, and so on.
Example:
const currentDate = new Date();
const yearUTC = currentDate.getUTCFullYear();
const monthUTC = currentDate.getUTCMonth();
console.log(`UTC Year: ${yearUTC}`);
console.log(`UTC Month: ${monthUTC}`);
By using these methods and UTC-related functionalities, JavaScript allows you to perform date and time operations in the UTC time zone, ensuring consistency across different time zones and facilitating accurate cross-system communication.
const currentDate = new Date();
const dayUTC = currentDate.getUTCDate();
console.log(`UTC Day: ${dayUTC}`);
getUTCMonth()
: ThegetUTCMonth()
method returns the month (0-11) according to the UTC time zone. Note that January is represented by 0, February by 1, and so on.
Example:
const currentDate = new Date();
const monthUTC = currentDate.getUTCMonth();
console.log(`UTC Month: ${monthUTC}`);
getUTCFullYear()
: ThegetUTCFullYear()
method returns the four-digit year according to the UTC time zone.
Example:
const currentDate = new Date();
const yearUTC = currentDate.getUTCFullYear();
console.log(`UTC Year: ${yearUTC}`);
These methods are similar to their non-UTC counterparts (getDate()
, getMonth()
, and getFullYear()
), but they retrieve the date components based on the UTC time zone instead of the local time zone. They are particularly useful when working with UTC-based calculations, conversions, or when dealing with dates in cross-time zone scenarios.
Using these methods allows you to work with date components in UTC format, ensuring consistency and accurate representation of time across different time zones.
const currentDate = new Date();
const formattedTime = currentDate.toLocaleTimeString();
console.log(formattedTime);
This will output the current time in a localized format based on the user’s browser settings.
Using external libraries: For more advanced time formatting options or when working extensively with dates and times, you can use third-party libraries such as Moment.js or date-fns.
Here’s an example using Moment.js:
const moment = require('moment');
const currentDate = moment();
const formattedTime = currentDate.format('HH:mm:ss');
console.log(formattedTime);
his will output the current time in the format “HH:mm:ss” (24-hour format).
Similarly, you can use libraries like date-fns or Luxon for formatting time in JavaScript. These libraries provide comprehensive formatting options and additional features for working with dates and times.
Note that Moment.js is considered a legacy project, and it is recommended to use modern alternatives like date-fns or Luxon for new projects.
const currentDate = new Date();
const dateString = currentDate.toDateString();
console.log(dateString);
This will output the date portion of the currentDate
in a human-readable format, such as “Wed Jul 14 2023”.
toTimeString()
: ThetoTimeString()
method is used to retrieve the time portion of aDate
object as a human-readable string, excluding the date.
Example:
const currentDate = new Date();
const timeString = currentDate.toTimeString();
console.log(timeString);
This will output the time portion of the currentDate
in a human-readable format, such as “13:45:30 GMT+0300 (Eastern European Summer Time)”.
Both toDateString()
and toTimeString()
methods provide localized string representations of the date and time based on the user’s browser settings. However, the specific format and language may vary depending on the user’s locale and browser implementation.
These methods are useful when you want to display or store only the date or time portion of a Date
object as a string, without including the other components.




Popular Category
Topics for You
Introduction
Html page 1
Html page 2
Html page3
Html page4
HTML Elements and structure
Html page 5
Html page 6
Html page 7
HTML Headings and Paragraphs
Html page 8
Html page 9
Html page 10
HTML Lists and Tables
Html page 11
Html page 12
Html page 13
HTML Forms and Input Fields
Html page 14
Html page 15
Html page 16
HTML Images and Media
Html page 17
Html page 18
HTML Links and Anchors
Html page 19
Html page 20
Html page 21
HTML Styles and Formatting
Html page 22
HTML Semantic Elements
Html page 23
Html page 24
HTML Attributes
Html page 25
Html page 26
HTML JavaScript Integration
Html page 27
Html page 28
Html page 29
Html page 30
HTML Document and Browser Support
Html page 31
Html page 32
HTML5 New Elements and Attributes
Html page 33
Html page 34
Html page 35
Html page 36
HTML Accessibility and Web Standards
Html page 37
Html page 38
Html page 39
HTML Responsive Design and Mobile Devices.
Html page 40
Html page 41
Html page 42
Introduction
Data Structure Page 1
Data Structure Page 2
Data Structure Page 3
Data Structure Page 4
Data Structure Page 5
Data Structure Page 6
Data Structure Page 7
Data Structure Page 8
String
Data Structure Page 9
Data Structure Page 10
Data Structure Page 11
Data Structure Page 12
Data Structure Page 13
Array
Data Structure Page 14
Data Structure Page 15
Data Structure Page 16
Data Structure Page 17
Data Structure Page 18
Linked List
Data Structure Page 19
Data Structure Page 20
Stack
Data Structure Page 21
Data Structure Page 22
Queue
Data Structure Page 23
Data Structure Page 24
Tree
Data Structure Page 25
Data Structure Page 26
Binary Tree
Data Structure Page 27
Data Structure Page 28
Heap
Data Structure Page 29
Data Structure Page 30
Graph
Data Structure Page 31
Data Structure Page 32
Searching Sorting
Data Structure Page 33
Hashing Collision
Data Structure Page 35
Data Structure Page 36
Go through our study material. Your Job is awaiting.