Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Working with UTC and Local Time | Advanced Time Handling and Modern Tools
Working with Dates and Times in JavaScript

bookWorking with UTC and Local Time

When working with dates and times in JavaScript, you need to understand the distinction between UTC (Coordinated Universal Time) and local time. UTC is a standard time that does not change with time zones or daylight saving time, while local time is determined by the user's computer settings and location. This difference is important because date and time calculations can yield different results depending on whether you use UTC or local time. For example, if you are building an application that serves users in multiple time zones, storing and manipulating dates in UTC helps maintain consistency and avoids confusion that could arise from local time differences.

12345678910
const date = new Date('2024-06-15T18:30:00Z'); // Accessing UTC components const utcYear = date.getUTCFullYear(); // 2024 const utcMonth = date.getUTCMonth(); // 5 (June, zero-based) const utcHour = date.getUTCHours(); // 18 console.log(`UTC Year: ${utcYear}`); console.log(`UTC Month: ${utcMonth}`); console.log(`UTC Hour: ${utcHour}`);
copy

JavaScript provides specific methods such as getUTCFullYear(), getUTCMonth(), and getUTCHours() to access the UTC components of a Date object, as shown above. These methods allow you to read the date and time values as they would appear in the UTC time zone, regardless of the user's local settings. If you want to convert between local time and UTC, you can use the corresponding local methods (getFullYear(), getMonth(), getHours()) alongside the UTC methods. This approach lets you compare or display dates in both formats. For instance, the same Date object might show a different hour when accessed with getHours() compared to getUTCHours(), depending on the user's time zone. Understanding how and when to use these methods helps you avoid errors and ensures your application displays date and time information accurately for all users.

question mark

What is the main difference between UTC and local time in JavaScript?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 1

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Suggested prompts:

Can you explain the difference between getUTC* and get* methods in JavaScript?

How do I convert a local time to UTC in JavaScript?

Why does getUTCMonth() return 5 for June?

Awesome!

Completion rate improved to 7.14

bookWorking with UTC and Local Time

Desliza para mostrar el menú

When working with dates and times in JavaScript, you need to understand the distinction between UTC (Coordinated Universal Time) and local time. UTC is a standard time that does not change with time zones or daylight saving time, while local time is determined by the user's computer settings and location. This difference is important because date and time calculations can yield different results depending on whether you use UTC or local time. For example, if you are building an application that serves users in multiple time zones, storing and manipulating dates in UTC helps maintain consistency and avoids confusion that could arise from local time differences.

12345678910
const date = new Date('2024-06-15T18:30:00Z'); // Accessing UTC components const utcYear = date.getUTCFullYear(); // 2024 const utcMonth = date.getUTCMonth(); // 5 (June, zero-based) const utcHour = date.getUTCHours(); // 18 console.log(`UTC Year: ${utcYear}`); console.log(`UTC Month: ${utcMonth}`); console.log(`UTC Hour: ${utcHour}`);
copy

JavaScript provides specific methods such as getUTCFullYear(), getUTCMonth(), and getUTCHours() to access the UTC components of a Date object, as shown above. These methods allow you to read the date and time values as they would appear in the UTC time zone, regardless of the user's local settings. If you want to convert between local time and UTC, you can use the corresponding local methods (getFullYear(), getMonth(), getHours()) alongside the UTC methods. This approach lets you compare or display dates in both formats. For instance, the same Date object might show a different hour when accessed with getHours() compared to getUTCHours(), depending on the user's time zone. Understanding how and when to use these methods helps you avoid errors and ensures your application displays date and time information accurately for all users.

question mark

What is the main difference between UTC and local time in JavaScript?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 1
some-alt