How to Convert String to Date in Javascript in dd-mm-yyyy Format

The answer to this question, “How to convert string to date in javascript in dd-mm-yyyy format?” is transforming a string to Date in JS, a crucial skill every programmer should know. Furthermore, you should be able to display a date object to make you a skilled developer. So, this blog will discuss converting a string to Date in dd-mm-yyyy format. Thereafter, let’s discuss “How do you convert String to Date in Javascript in dd-mmm-yyyy Format?”

Methods for converting string to Date are: 

  • Method 1: The Javascript Date Object
  • Method 2: The parse() Method
  • Method 3: Using a Split Function
  • Method 4: The Day.js Library

Therefore, Javascript has many applications in the programming world. So, If you learn these methods, you can display time in HTML using Javascript and know how to create a calendar in HTML using Javascript. Also, this blog will discuss the methods for Displaying a Date Object in the dd-mmm-yyyy Format. Without further ado, let’s get started..

How to Convert dd-MMMM-YYYY String to Date in Javascript

This section will cover “How to convert string to date in javascript in dd-mm-yyyy format?”

Method 1: The Javascript Date Object

There are many objects in Javascript, among which the date object is used for this purpose. You can also convert these Javascript objects to JSON and utilize it in your projects. Therefore, Javascript offers you an integrated Date class that you can use in your projects to transform Date and time.date objects

A new Date() constructor allows you to make a new date object. When you run the Date() constructor, it automatically gives you the present Date and time. For example:

let currentDate = new Date();

console.log(currentDate);

let HappyNewYear = new Date(“01-Jan-2025 UTC”)

console.log(HappyNewYear) 

Output:

2024-02-09T03:55:30.129Z

2025-01-01T00:00:00.000Z

The above output has two results: the first is the current Date, whereas the second is the new Date, which we have placed in the new Date () constructor. Similarly, we can also transform dd-mmm-yyyy into a date object. For example:

let HappyNewYear = new Date(“01/Jan/2025 UTC”)

console.log(HappyNewYear) 

Output:

2025-01-01T00:00:00.000Z

However, you cannot transform and get a new date in dd/mm/yyyy format. It will give back an error with an invalid date or a wrong date object. 

Method 2: The parse() Method

The next method of this, “How to convert string to date in javascript in dd-mm-yyyy format?” is the parse method. parse methodThe Date.parse() function is a stable process used in Javascript. You can transform a string into a date object by determining the content of the string. However, it will give you a timestamp showing the number of milliseconds from the Unix epoch(01 Jan. 1970). For example:

let dateString = “07-Nov-2023”;

let timestamp = Date.parse(dateString);

console.log(timestamp)

Output:

1699315200000

Therefore, to convert the above milliseconds, we will use the help of the new Date in js for dd/mm/yyyy conversion. For example: 

let dateString = “07-Nov-2023 UTC”;

let timestamp = Date.parse(dateString);

let dateObject = new Date(timestamp);

console.log(dateObject)

Output:

2023-11-07T00:00:00.000Z

See Also: How To Run Javascript In Visual Studio Code?

Method 3: Using a Split Function

If you have noticed, the above methods can’t be used to transform dd-mm-yyyy or dd/mm/yyyy into a date object. split functionTherefore, we have to utilize the split function. By the name, you must have understood that its function is to split. It will branch the string into day, month, and year. Afterward, we will use the js new Date for dd/mm/yyyy and transform it into a date object. For example:

function dateSplit(dateString){

    const dateParts = dateString.split(“-“);

    return new Date(Date.UTC(dateParts[2], dateParts[1]-1, dateParts[0]))

}

console.log(dateSplit(“01-02-2025”))

Output:

2025-02-01T00:00:00.000Z

For dd/mm/yyyy format, we must modify the split function and write “/” instead of “-” For example:

function customDateSplit(dateStringParam){

    const dateComponents = dateStringParam.split(“/”);

    return new Date(Date.UTC(dateComponents[2], dateComponents[1]-1, dateComponents[0]));

}

console.log(dateSplit(“25/12/2023”))

Output:

2023-12-25T00:00:00.000Z

Method 4: The Day.js Library

To convert strings to Dates in Javascript, we can use the Day.js library. But before using it, you must ensure the library is installed.day js For installation, write this code in your terminal:

npm install days 

Now, after installation, let’s convert dd-mmm-yyyy. Run the following code:

const dayjs = require(“dayjs”)

const dateObj = dayjs(“25-Dec-2025”, “dd-mmm-yyyy”)

console.log(dateObj)

This code will give you the desired date object. Furthermore, you can write this code to transform the string dd-mm-yyyy into a date object. Also, It will provide you with the desired output:

const customDayjs = require(“dayjs”);

const customDateObj = customDayjs(“25/12/2025”, “dd-mm-yyyy”);

Displaying a Date Object in the dd-mmm-yyyy Format

We have seen how to convert strings to dates in Javascript in dd-mm-yyyy format. This section will focus on displaying a date object in the dd-mmm-yyyy format.

See Also: How To Read A JSON File In Javascript?

Parse the string into a Date object

Parsing the string into a date object is another way to display the date object. So, The get() methods from the date library will help you convert. It will fetch the date components when transforming a date object to a string in a particular format. parse string into a date objectFurthermore, To get the desirable format of dd-mmm-yyyy, you must retrieve and integrate the applicable information. For instance:

function customToString(customDateObject){

    let dayValue = customDateObject.getDate();

    let monthIndexValue = customDateObject.getMonth();

    let yearValue = customDateObject.getFullYear();

    let monthNamesArray = [“Jan”, “Feb”, “Mar”, “Apr”, “May”, “Jun”, “Jul”, “Aug”, “Sep”, “Oct”, “Nov”, “Dec”];

    let monthValue = monthNamesArray[monthIndexValue];

    return `${dayValue}-${monthValue}-${yearValue}`;

}

const customDate = new Date(“2024/02/16 UTC”);

console.log(customToString(customDate));

Output:

16-Feb-2024

Lastly, in the above code, the toString function grasps a date object. Also, It fetches the day, month, and year from it, integrating all of them in the end. 

The ToLocaleString Function

So, This method can display a date object in Javascript. Also, this function has two elective parameters: locales and options.tolocalestring method The locales framework helps you describe the language and region for conversion, whereas the options framework helps you to customize the display. For example:

let newDateObj = new Date(“2023/11/07 UTC”);

let dateFormatOptions = { day: ‘2-digit’, month: ‘short’, year: ‘numeric’ };

let formattedNewDate = newDateObj.toLocaleString(‘en-gb’, dateFormatOptions);

console.log(formattedNewDate);

Output: 

07 Nov 2023

So, In the above code, the options framework describes the day as two digits, the month as short, and the year as numeric. Also, the locales framework describes the region as Great Britain. The new Date in javascript is in the dd/mm/yyyy format, which is then transformed into dd/mmm/yyyy.

Using the day.js Library

We used the day library earlier while transforming the string to Date. Similarly, we can also use it to display date objects.date.js method For instance: 

const dayjs = require(“dayjs”);

const date = dayjs(“2023-06-29 UTC”);

const formatted = date.format(“DD-MMM-YYYY”);

console.log(formatted);

Output:

29-Jun-2023

FAQs

How to change the date format to dd mm yyyy in JavaScript?

You can use the help of getDate(), getMonth(), and getFullYear() to change the date format to dd-mm-yyyy.

What is the format of string date in JavaScript?

The string date format in Javascript is YYYY-MM-DDTHH:mm:ss.sssZ, where Z is used to describe the timezone.

What is the new date () in JavaScript?

A new Date() constructor allows you to make a new date object. When you run the Date() constructor, it automatically returns the present date and time.

How to get full date in JavaScript?

You can use the Date() object to get the full date in Javascript. It will give you with the full date and time.

Conclusion

In conclusion, we learned how to convert strings to dates in Javascript in the dd-mm-yyyy format. With the help of the above methods, we can also convert it into other formats as per our requirements. So, In most methods, we have used js new Date for dd/mm/yyyy format and transformed other formats.

Hence, you can learn how to convert an array to a map in Javascript to increase your skills as a programmer. But before that, ensure you know how to declare an empty array in JavaScript, as it’s the basics.

Leave a Comment