Date to ISO Format With Timezone

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will learn how to convert a date into the extended ISO format (ISO 8601), including timezone offset. We will use the Date.prototype.getTimezoneOffset() method to get the timezone offset and reverse it. Then, we will define a helper function to normalize any passed number to an integer and pad it to 2 digits using String.prototype.padStart(). Finally, we will use the built-in methods in the Date prototype to build the ISO 8601 string with timezone offset. By the end of this lab, you will have a better understanding of how to manipulate dates in JavaScript.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript(("`JavaScript`")) -.-> javascript/AdvancedConceptsGroup(["`Advanced Concepts`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") javascript/AdvancedConceptsGroup -.-> javascript/template_lit("`Template Literals`") subgraph Lab Skills javascript/variables -.-> lab-28451{{"`Date to ISO Format With Timezone`"}} javascript/data_types -.-> lab-28451{{"`Date to ISO Format With Timezone`"}} javascript/arith_ops -.-> lab-28451{{"`Date to ISO Format With Timezone`"}} javascript/comp_ops -.-> lab-28451{{"`Date to ISO Format With Timezone`"}} javascript/template_lit -.-> lab-28451{{"`Date to ISO Format With Timezone`"}} end

Converting Dates to ISO Format with Timezone

To convert a date to the extended ISO format (ISO 8601), including the timezone offset, follow these steps:

  1. Open the Terminal/SSH and enter node to begin coding.
  2. Use Date.prototype.getTimezoneOffset() to get the timezone offset and reverse it. Store its sign in diff.
  3. Define a helper function, pad(), that normalizes any passed number to an integer using Math.floor() and Math.abs() and pads it to 2 digits, using String.prototype.padStart().
  4. Use pad() and the built-in methods in the Date prototype to build the ISO 8601 string with timezone offset.

Here's the code you can use:

const toISOStringWithTimezone = (date) => {
  const tzOffset = -date.getTimezoneOffset();
  const diff = tzOffset >= 0 ? "+" : "-";
  const pad = (n) => `${Math.floor(Math.abs(n))}`.padStart(2, "0");
  return (
    date.getFullYear() +
    "-" +
    pad(date.getMonth() + 1) +
    "-" +
    pad(date.getDate()) +
    "T" +
    pad(date.getHours()) +
    ":" +
    pad(date.getMinutes()) +
    ":" +
    pad(date.getSeconds()) +
    diff +
    pad(tzOffset / 60) +
    ":" +
    pad(tzOffset % 60)
  );
};

Use the function toISOStringWithTimezone() with a new Date() object as the argument to get the date in ISO format with timezone offset. For example:

toISOStringWithTimezone(new Date()); // '2020-10-06T20:43:33-04:00'

Summary

Congratulations! You have completed the Date to ISO Format With Timezone lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like