by Sai gowtham

How to format the date and time in JavaScript

In the last tutorial, we have seen how to get the current date and time.In this tutorial, we are going to learn about how to format the date and time in JavaScript.

JavaScript Date object comes with different type of date and time methods.But we are using toLocaleDateString() and toLocaleTimeString() methods.

Formatting date

The toLocaleDateString() method accepts two arguments, which are locales and options.

Locale means the type of local language you need to format.

These are some options we are using to format the date.

weekday: possible values are narrow short, long.

year: possible values are numeric, 2-digit.

month: possible values are numeric, 2-digit, narrow, short, long.

day: possible values are numeric, 2-digit.

Example

const options = {
     weekday: "long",
     year: "numeric",
     month:"long",
     day:"numeric"
};

new Date().toLocaleDateString("en-US",options);
// output - > "Saturday, December 8, 2018"

new Date().toLocaleDateString("hi",options);
// output -> "शनिवार, 8 दिसंबर 2018"

new Date().toLocaleDateString("ja-JP-u-ca-japanese",options);
// output -> "30年12月8日土曜日"

Now we are changing the option values from long to short.

const options = {
     weekday: "short",
     year: "numeric",
     month:"short",
     day:"2-digit"
     }

new Date().toLocaleDateString("en-US",options);

//output ->  "Sat, Dec 08, 2018"

const options = {
     year: "2-digit",
     month:"2-digit",
     day:"2-digit"
     }

new Date().toLocaleDateString("en-US",options);
// output  "12/08/18"


const options = {
       weekday:"short",
       day:"2-digit"
    }

new Date().toLocaleDateString("en-US",options);

 //  "08 Sat"

Formatting time

The toLocaleTimeString() method also accepts two arguments, locales and options.

These are some available options.

timeZone : Iana time zones List

hour12 : true or false.(true is 12 hour time ,false is 24hr time)

hour: numeric, 2-digit.

minute: numeric, 2-digit.

second: numeric, 2-digit.

const options = {
       timeZone:"Africa/Accra",
       hour12 : true,
       hour:  "2-digit",
       minute: "2-digit",
      second: "2-digit"
    };

new Date().toLocaleTimeString("en-US",options);

//-> Africa timezone with 12hr time  "8:09:19 AM"
const options = {
       timeZone:"Asia/Kolkata",
       hour12 : false,
       hour:  "2-digit",
       minute: "2-digit",
      second: "2-digit"
    }

new Date().toLocaleTimeString("en-US",options);

//Asia/Kolkata timezone with 24hr time  "13:42:31"
const options = {
       timeZone:"Canada/Central",
       hour12 : true,
       hour:  "numeric",
       minute: "numeric",seconds:"numeric"
    }

new Date().toLocaleTimeString("en-US",options);

// Canada/Central timezone -> "2:17 AM"

You can also get only hour.

const options = {
       timeZone:"Canada/Central",
       hour:  "numeric",
    }

new Date().toLocaleTimeString("en-IN",options);

//output   "2 AM"


new Date().toLocaleTimeString("ko-KR",options);

//output  "오전 2시"

You can also read, how to get a current year in JavaScript.

Happy coding.

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2021 (Beginner + Advanced)
JavaScript - The Complete Guide 2021 (Beginner + Advanced)
64,026 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
152,857 students enrolled
48.5 hours of video content
View Course