Skip to main content

Date Formatting in JavaScript: Converting Dates to Different Formats with Examples

Working with dates in JavaScript can be a bit tricky, especially when it comes to formatting them in different ways. In this blog post, we'll explore how to convert JavaScript dates into different formats of date, month, and year, specifying both full and short names of months and days, using examples with clear and concise language.

JavaScript's built-in Date object provides a number of methods that make working with dates easier. However, it can be difficult to remember the exact syntax needed to format dates in a particular way. Here are some common formats you may encounter when working with dates:

dd/mm/yyyy - day/month/year

mm/dd/yyyy - month/day/year

yyyy-mm-dd - year/month/day


To format a date in a particular way, we can use the toLocaleDateString() method. This method takes two arguments: a locale and an options object. The locale specifies the language and region to use for formatting, while the options object specifies the specific formatting options.


1) Format date as "dd/mm/yyyy" with short month name

To format a date as "dd/mm/yyyy" (day/month/year) with the short name of the month, we can use the following code:

const date = new Date();

const options = { day: '2-digit', month: 'short', year: 'numeric' };

const formattedDate = date.toLocaleDateString('en-GB', options);

console.log(formattedDate);

In this code, we create a new Date object to represent the current date and time. We then specify the options object with day: '2-digit', month: 'short', and year: 'numeric'. This tells JavaScript to format the date with two-digit values for the day, the short name of the month (e.g. "Jan"), and the full year. The resulting output should be a string in the format "dd/MM/yyyy".


2) Format date as "mm/dd/yyyy" with full month name

To format a date as "mm/dd/yyyy" (month/day/year) with the full name of the month, we can use the following code:

const date = new Date();

const options = { month: 'long', day: '2-digit', year: 'numeric' };

const formattedDate = date.toLocaleDateString('en-US', options);

console.log(formattedDate);


In this code, we use the en-US locale to specify that we want the date formatted for English speakers in the United States. We then specify month: 'long', day: '2-digit', and year: 'numeric' to format the date with the full name of the month (e.g. "January"), two-digit values for the day, and the full year. The resulting output should be a string in the format "January 01, 2022".


3) Format date as "yyyy-mm-dd" with full day name

To format a date as "yyyy-mm-dd" (year/month/day) with the full name of the day, we can use the following code:

const date = new Date();
const options = { year: 'numeric', month: '2-digit', day: '2-digit', weekday: 'long' };
const formattedDate = date.toLocaleDateString('sv-SE', options);
console.log(formattedDate);

In this code, we use the sv-SE locale to specify that we want the date formatted for Swedish speakers in Sweden. We then specify year: 'numeric', month: '2-digit', day: '2-digit', and weekday: 'long' to format the date accordingly. 


4) Format date as "dd Mmm yyyy"

To format a date as "dd Mmm yyyy" (e.g. 03 Sept 2022), we can use the following code:

const date = new Date();

const options = { day: '2-digit', month: 'short', year: 'numeric' };

const formattedDate = date.toLocaleDateString('en-US', options);

console.log(formattedDate); // "28 Mar 2023"


In this code, we create a new Date object to represent the current date and time. We then specify the options object with day: '2-digit', month: 'short', and year: 'numeric'. This tells JavaScript to format the date with two-digit values for the day, the short name of the month (e.g. "Sept"), and the full year. The resulting output should be a string in the format "dd Mmm yyyy".


5) Format date as "dd Month yyyy"

To format a date as "dd Month yyyy" (e.g. 03 September 2022), we can use the following code:

const date = new Date();

const options = { day: '2-digit', month: 'long', year: 'numeric' };

const formattedDate = date.toLocaleDateString('en-US', options);

console.log(formattedDate); // "28 March 2023"


 In this code, we again create a new Date object to represent the current date and time. We then specify the options object with day: '2-digit', month: 'long', and year: 'numeric'. This tells JavaScript to format the date with two-digit values for the day, the full name of the month (e.g. "September"), and the full year. The resulting output should be a string in the format "dd Month yyyy".


6) Format date as "Dd Mmm yyyy"

To format a date as "Dd Mmm yyyy" (e.g. Mon 28 Mar 2023), we can use the following code:

const date = new Date();
const options = { weekday: 'short', day: '2-digit', month: 'short', year: 'numeric' };
const formattedDate = date.toLocaleDateString('en-US', options);
console.log(formattedDate); // "Mon 28 Mar 2023"

In this code, we create a new Date object to represent the current date and time. We then specify the options object with weekday: 'short', day: '2-digit', month: 'short', and year: 'numeric'. This tells JavaScript to format the date with the short name of the weekday (e.g. "Mon"), two-digit values for the day, the short name of the month, and the full year. The resulting output should be a string in the format "Dd Mmm yyyy".

Comments

Popular posts from this blog

Comparison of 8-bit (8085),16-bit (8086) and 32-bit (80386) microprocessor

Microprocessor is the heart of the computer and the power of microprocessor is depends on the amount of data handled by it. Microprocessor is manufactured into the lightweight IC structure. So mostly when the study goes on about revolution of microprocessors or something about its family the question is always asked, "what is the difference between them" so we have sorted out some points about their differences as below: Sr. No. Parameters 8085 8086 80386 1. Year of Introduction 1977 1978 1985 2. Data Bus 8 bit 16 bit 32 bit 3. Address Bus   16 bit 20 bit 32 bit 4. Physical Memory   16 KB 1 MB 4 GB 5. Register size 8 bit 16 bit 32 bit 6. No. of trans...

Practical 1: Submit a profile summary (about 500 words) of a successful entrepreneur indicating milestone achievements.

Answer: (Write one of the following three profiles) 1) Entrepreneur Name : Ashneer Grover Ashneer Grover is a prominent Indian entrepreneur who has made a name for himself in the world of business. He is the founder and CEO of BharatPe, a fintech startup that is revolutionizing the payments landscape in India. Grover has achieved several business milestones throughout his career, which has made him a notable figure in the startup world. Grover graduated from Indian Institute of Technology Delhi with a degree in chemical engineering. He began his career as a management consultant with A.T. Kearney, where he worked for five years. After that, he joined Bharti Airtel, one of the largest telecommunications companies in India, where he served as the CFO of the company's DTH and IPTV divisions. In 2015, Grover became the CEO of Grofers, an online grocery delivery service based in India. Under his leadership, Grofers expanded its operations to several Indian cities and raised more tha...

Practical 2: Undertake SWOT analysis to arrive at your business idea of a product/service.

SWOT analysis is a framework used to evaluate the strengths, weaknesses, opportunities, and threats of a business. Here are some points to consider when conducting a SWOT analysis for your business: Strengths :  These are the internal factors that give a business an advantage over its competitors. Strengths can include unique skills or resources, a strong brand reputation, a loyal customer base, or a talented team. ·      What advantages does your business have over its competitors? ·      What unique skills, resources, or assets does your business possess? ·      What are your business's competitive advantages? ·      What positive feedback have you received from customers or clients? Weaknesses: These are the internal factors that put a business at a disadvantage compared to its competitors. Weaknesses can include limited resources, lack of brand recognition, poor management, or a high emp...