Skip to content
the poor coder

convert long date to short date javascript

Poor Man
Published
Reading time
2 min read
On this page

Convert Long Date to Short Date in JavaScript#

If you are working with dates in JavaScript, you may need to convert a long date format to a short date format. There are several ways to do this, depending on your requirements. Here are some examples:

Example 1: Using the toLocaleDateString() Method#

The easiest way to convert a date to a short format is to use the built-in toLocaleDateString() method. This method returns the date as a string, using the browser's default locale and formatting options.

Text

const longDate = new Date('2021-10-22T12:45:00');
const shortDate = longDate.toLocaleDateString();
console.log(shortDate); // Output: 10/22/2021

In this example, we create a new Date object with a long date string, and then use toLocaleDateString() to convert it to a short date string. The output is in the format MM/DD/YYYY, which is the default for the United States.

Example 2: Using the Intl.DateTimeFormat API#

If you need more control over the formatting of the short date, you can use the Intl.DateTimeFormat API. This API allows you to specify the locale, date style, time style, and other options.

Text

const longDate = new Date('2021-10-22T12:45:00');
const formatter = new Intl.DateTimeFormat('en-US', {dateStyle: 'short'});
const shortDate = formatter.format(longDate);
console.log(shortDate); // Output: 10/22/21

In this example, we create a new DateTimeFormat object with the options for the United States and a short date style. We then use the format() method to convert the long date to a short date string. The output is in the format MM/DD/YY.

Example 3: Using String Manipulation#

If you don't want to use built-in methods or APIs, you can convert a long date to a short date by manipulating the string directly.

Text

const longDate = 'Friday, October 22, 2021';
const shortDate = longDate.replace(', 20', '/');
console.log(shortDate); // Output: Friday, October 22/21

In this example, we start with a long date string and use the replace() method to replace the comma and the year with a forward slash. This gives us a short date in the format DD/MM/YY. Note that this method is not very flexible and may not work for all date formats.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC