Skip to content
the poor coder

javascript get past time in specific format

Poor Man
Published
Reading time
2 min read
On this page

Javascript Get Past Time in Specific Format#

As a web developer, I often come across situations where I need to display dates and times on my web pages in a specific format, especially the past time. Luckily, Javascript provides a built-in method to achieve this, which is the toLocaleString() method.

Using toLocaleString() Method#

The toLocaleString() method converts a date object into a string using a specified format based on the user's local time zone. We can use this method to get the past time in a specific format as well.

Let's say that we want to display the past time in the format of "dd/mm/yyyy hh:mm:ss" (e.g. 25/12/2021 10:30:00). Here's how we can do it:

JavaScript

            const pastDate = new Date("2021-12-25T10:30:00");
            const formattedPastDate = pastDate.toLocaleString();
            console.log(formattedPastDate);
        

In the above code, we create a new Date object with the specific past time we want to format. We then use the toLocaleString() method to convert the date object into a string with our desired format. Finally, we log the formatted date string to the console.

Using External Libraries#

If you don't want to use the built-in toLocaleString() method or want more advanced formatting options, you can use external libraries like Moment.js or Date-fns.

Moment.js is a popular library for parsing, manipulating, and formatting dates and times in Javascript. Here's how we can use Moment.js to format the past time:

JavaScript

            const pastDate = new Date("2021-12-25T10:30:00");
            const formattedPastDate = moment(pastDate).format("DD/MM/YYYY HH:mm:ss");
            console.log(formattedPastDate);
        

In the above code, we first import the Moment.js library. We then create a new Date object with the specific past time we want to format. We use the moment() method to convert the date object into a Moment object. Finally, we use the format() method to format the Moment object into our desired format.

Date-fns is another popular library for manipulating and formatting dates and times in Javascript. Here's how we can use Date-fns to format the past time:

JavaScript

            import { format } from "date-fns";

            const pastDate = new Date("2021-12-25T10:30:00");
            const formattedPastDate = format(pastDate, "dd/MM/yyyy HH:mm:ss");
            console.log(formattedPastDate);
        

In the above code, we first import the format() method from the Date-fns library. We then create a new Date object with the specific past time we want to format. Finally, we use the format() method to format the date object into our desired format.

Conclusion#

In conclusion, displaying past time in a specific format in Javascript can be achieved using built-in methods like toLocaleString(), or external libraries like Moment.js and Date-fns. The choice of method depends on the specific formatting requirements and personal preferences.

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