Sling Academy
Home/JavaScript/JavaScript: 3 ways to truncate the time portion of a date string

JavaScript: 3 ways to truncate the time portion of a date string

Last updated: March 07, 2023

When building applications with JavaScript, there might be scenarios where you want to remove the time portion from a date string, such as:

  • Displaying dates: When displaying dates in a UI, you may only want to show the date part and not the time.
  • Storing dates: When storing the prices of stocks, you usually don’t need the time part.

This concise, example-based article will show you a couple of different ways to do so.

Using string manipulation

Example:

// Input date string in ISO format
const dateString = '2023-03-07T14:30:00.000Z';

// Truncate time portion
const truncatedDate = dateString.split('T')[0];

console.log(truncatedDate);

Output:

2023-03-07

Using Date object

Example:

// Input date string in YYYY-MM-DD HH:MM:SS format
const dateString = '2023-12-30 12:00:00';

// Convert to Date object
const dateObj = new Date(dateString);

// Get only the date part
const truncatedDate =
  dateObj.getFullYear() +
  '-' +
  (dateObj.getMonth() + 1) +
  '-' +
  dateObj.getDate();

console.log(truncatedDate);

Output:

2023-12-30

Using Regular Expressions

You can also use a regular expression to match the time portion of the given date string and replace it with an empty string.

Example:

// Input date string
const dateString = '2024-03-07T14:30:00.000Z';

// Truncate time portion using regular expressions
const truncatedDate = dateString.replace(/T.+/, '');

console.log(truncatedDate);

Output:

2024-03-07

That’s it. If you have any questions, please comment. Happy coding & have a nice day!

Next Article: JavaScript: 2 Ways to Clone (Deep Copy) a Date Object

Previous Article: JavaScript: How to Check if 2 Date Ranges Overlap (2 Ways)

Series: Date and Time in JavaScript: Basic to Advanced Tutorials

JavaScript

You May Also Like

  • Handle Zoom and Scroll with the Visual Viewport API in JavaScript
  • Improve Security Posture Using JavaScript Trusted Types
  • Allow Seamless Device Switching Using JavaScript Remote Playback
  • Update Content Proactively with the JavaScript Push API
  • Simplify Tooltip and Dropdown Creation via JavaScript Popover API
  • Improve User Experience Through Performance Metrics in JavaScript
  • Coordinate Workers Using Channel Messaging in JavaScript
  • Exchange Data Between Iframes Using Channel Messaging in JavaScript
  • Manipulating Time Zones in JavaScript Without Libraries
  • Solving Simple Algebraic Equations Using JavaScript Math Functions
  • Emulating Traditional OOP Constructs with JavaScript Classes
  • Smoothing Out User Flows: Focus Management Techniques in JavaScript
  • Creating Dynamic Timers and Counters with JavaScript
  • Implement Old-School Data Fetching Using JavaScript XMLHttpRequest
  • Load Dynamic Content Without Reloading via XMLHttpRequest in JavaScript
  • Manage Error Handling and Timeouts Using XMLHttpRequest in JavaScript
  • Handle XML and JSON Responses via JavaScript XMLHttpRequest
  • Make AJAX Requests with XMLHttpRequest in JavaScript
  • Customize Subtitle Styling Using JavaScript WebVTT Integration