Formatting Dates in TypeScript to UTC

A guide on how to format dates in TypeScript to the specific format YYYY-MM-DDTHH:mm:ss+00:00.

Blog cover image
2101050's avatar
2101050
82 views

To format a date in TypeScript to the specific format YYYY-MM-DDTHH:mm:ss+00:00, you can use the following code snippet:

Typescript
1function formatDate(date: Date): string { 2 // Get the components of the date 3 const year = date.getUTCFullYear(); 4 const month = String(date.getUTCMonth() + 1).padStart(2, '0'); // Months are zero-based 5 const day = String(date.getUTCDate()).padStart(2, '0'); 6 const hours = String(date.getUTCHours()).padStart(2, '0'); 7 const minutes = String(date.getUTCMinutes()).padStart(2, '0'); 8 const seconds = String(date.getUTCSeconds()).padStart(2, '0'); 9 10 // Format the output 11 return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}+00:00`; 12} 13 14// Example usage 15const newDate = new Date('2023-10-26T04:35:45Z'); // UTC time 16const formattedDate = formatDate(newDate); 17console.log(formattedDate); // Output: 2023-10-26T04:35:45+00:00 18

Explanation:

  1. Creating a Date Object: We create a new Date object with the desired date and time in UTC.
  2. Extracting Date Components:
    • We use getUTCFullYear(), getUTCMonth(), getUTCDate(), getUTCHours(), getUTCMinutes(), and getUTCSeconds() to get the respective components of the date in UTC.
    • We use padStart(2, '0') to ensure that month, day, hours, minutes, and seconds are always two digits.
  3. Formatting the Output: We concatenate the components into the desired format.

This will give you the output in the specified format, including the timezone offset.

Recommended Articles

Discover more articles you might find interesting

Implementing LangGraph REST API with FastAPI
Technical Insights

Implementing LangGraph REST API with FastAPI

This guide provides a comprehensive implementation plan for building a LangGraph REST API using FastAPI, covering environment setup, agent definitions, endpoint creation, testing, and deployment.

2101050
Jun 18
149
Read More
DeepSite v2 Practical Guide
Technical Insights

DeepSite v2 Practical Guide

A comprehensive guide to DeepSite v2, covering its features, installation, and advanced workflows.

2101050
Jun 21
110
Read More
Fastify OpenTelemetry: Logging, Metrics, and Tracing in Practice
Technical Insights

Fastify OpenTelemetry: Logging, Metrics, and Tracing in Practice

Learn how to implement logging, metrics, and tracing in Fastify using OpenTelemetry.

2101050
Jul 11
105
Read More
Creating Diverse Logo Designs with Flux Model and ComfyUI
Technical Insights

Creating Diverse Logo Designs with Flux Model and ComfyUI

Learn to leverage the Flux model and ComfyUI for unique logo designs through effective prompts and examples.

2101050
Jan 10
92
Read More
Implementing a Custom Chat Model with LangChain
Technical Insights

Implementing a Custom Chat Model with LangChain

This guide provides a comprehensive blueprint for creating a custom chat model by subclassing LangChain's BaseChatModel, including configuration, method overrides, and error handling.

2101050
Jun 17
77
Read More
EAS Local Build Expo for Windows: Step-by-Step Guide, Troubleshooting, and Real-World Cases
Technical Insights

EAS Local Build Expo for Windows: Step-by-Step Guide, Troubleshooting, and Real-World Cases

Explore the complete process of setting up and troubleshooting EAS Local Builds on Windows using WSL, including practical examples and best practices.

2101050
Jul 08
77
Read More