Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Programming»JavaScript»How to Get Current Date & Time in JavaScript

    How to Get Current Date & Time in JavaScript

    By RahulJuly 26, 20182 Mins ReadUpdated:August 6, 2022

    Question – How do I get the current date and time in JavaScript? How to get the date in Y-m-d format in JavaScript? How do I get time in H:i:s format in JavaScript?

    Advertisement

    Time is an important part of our life and we cannot avoid it. In our daily routine, we need to know the current date or time frequently. JavaScript provides a global variable Date which helps you to get the current Date & Time in JavaScript. However, it won’t give you accurate information and rather return the local computer time instead of UTC time. To get accurate Date & Time in JavaScript, you need to use different APIs provided by JavaScript itself. You can also get the date and time in your formats like YYYY-MM-DD and HH:MM:SS formats.

    This article explains all about getting the current Date & Time in JavaScript with examples and best practices.

    Get Current Date & Time in JavaScript

    Use the Date() function to create an object in JavaScript with the current date and time. This provides output in the UTC timezone.

    1
    var today = new Date();  

    1. Current Date in JavaScript

    Use the following script to get the current date using JavaScript in “Y-m-d” format.

    1
    2
    var today = new Date();
    var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();

    • getFullYear() – Provides current year like 2022.
    • getMonth() – Provides current month values 0-11. Where 0 for Jan and 11 for Dec. So added +1 to get the result.
    • getDate() – Provides day of the month values 1-31.

    2. Current Time in JavaScript

    Use the following script to get the current time using JavaScript in “H:i:s” format.

    1
    2
    var today = new Date();
    var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();

    • getHours() – Provides current hour between 0-23.
    • getMinutes() – Provides current minutes between 0-59.
    • getSeconds() – Provides current seconds between 0-59.

    3. Current Date & Time Both in JavaScript

    Use the following script to get the current date and time using JavaScript in the “Y-m-d H:i:s” format. You can simply combine the output of the above JavaScript code in one variable as below:

    1
    2
    3
    4
    5
    6
    var today = new Date();
    var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
    var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
    var dateTime = date+' '+time;
     
    console.log(dateTime)

    Output
    2022-8-4 11:0:38

    date date and time javascript js
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Bash – How to Get Future Date and Time

    How to list all collections in MongoDB database

    What is difference between var, let and const in JavaScript?

    What is difference between var, let and const in JavaScript?

    View 31 Comments

    31 Comments

    1. B00D210 on May 1, 2022 6:11 pm

      ok but how can i get this dynamicl i mean to change this value on the website second by seconds.. need i use a API? 🙁

      Reply
      • Mahdi Askarpoor on August 5, 2022 3:26 pm

        Actually No.
        You can update date & time with setTimeout() method every secound.

        Reply
    2. wolf225 on February 25, 2022 12:27 pm

      Thanks

      Reply
    3. halim on October 13, 2021 9:41 am

      Thanks for sharing, works veery well but how do i make the time count automatically without having to refresh

      Reply
    4. TATHAGAT DALAI on July 28, 2021 11:36 am

      thanks

      Reply
    5. Iram Sanego on February 14, 2021 4:33 pm

      i want to put the time hrs mins in a graphs x axis.. how do i do it

      Reply
      • OSTATNI SPRAWIEDLIWY on March 25, 2021 9:13 pm

        IRAM SANEGO:

        Try Chartjs

        Reply
      • kowshi on April 15, 2022 9:18 am

        useful

        Reply
    6. Sushma on February 11, 2021 5:31 am

      How to get ddmmyyhhmmss?

      Reply
    7. Linda T. on July 20, 2020 10:03 pm

      Never mind. I figured it out. Thanks to all of you !!

      Reply
    8. Farooq Ahmed on July 16, 2020 10:27 pm

      How to get Day, not in value like actual day Mon, Tues from Date object?

      Reply
    9. yghu on July 9, 2020 8:05 am

      C00L

      Reply
    10. Marc Stager on January 27, 2020 10:10 pm

      Works, but doesn’t update (time doesn’t change)
      Also how can time be expressed in 12 hour, AM PM format?

      Reply
      • Muhammad Bilal Mohib-ul-Nabi on June 27, 2020 3:12 am

        Works well and update on page refresh or if you have used a button to display it like
        Time

        function showTime()
        {
        var today = new Date();
        var date = today.getFullYear()+’-‘+(today.getMonth()+1)+’-‘+today.getDate();
        var time = today.getHours() + “:” + today.getMinutes() + “:” + today.getSeconds();
        var dateTime = date+’ ‘+time;
        var show=document.getElementById(“show”);
        show.innerHTML=input;
        }

        //Now if you will click the button time will update

        Reply
    11. MuthuKumar on December 3, 2019 4:41 am

      var date = new Date().toJSON().slice(0,10);
      var time = new Date().toJSON().slice(11,19)
      var dateTime = date+’ ‘+time;

      Reply
    12. MuthuKumar on December 3, 2019 4:35 am

      new Date().toJSON().slice(0,10)

      Reply
    13. spyrosko on October 13, 2019 12:41 am

      This was very clear and helped me a lot. Much appreciated.

      Reply
    14. SZABOLCS on September 24, 2019 12:40 pm

      Also, you can use String(today.getMinutes()).padStart(2, ‘0’);

      Reply
      • Zolisa Welani on October 10, 2019 2:09 pm

        Thank you Sir!

        Reply
    15. Filip on August 29, 2019 9:15 pm

      This way when the minute is < 10 it will be displayed as e.g. '10:03' and not '10:3'
      if(today.getMinutes() < 10){
      var time = today.getHours() + ':0' + today.getMinutes();
      }
      enjoy..

      Reply
      • Linda Turshman on July 20, 2020 10:00 pm

        Hi Filip~ Where do I insert this additional code?

        Thanks. Linda (coding newbie)

        Reply
    16. John on August 15, 2019 1:41 pm

      There is just one issue when printing a time < 10. e.g. '11:3' instead of '11:03'.

      Reply
    17. Juan Loco on August 8, 2019 7:15 pm

      Thaks

      Reply
    18. RISKI on July 18, 2019 10:02 am

      thanks

      Reply
    19. Wajid Shehzad on July 4, 2019 10:45 am

      Very informative. Thanks to share knowledge with us

      Reply
    20. Aysha Perween on May 23, 2019 8:42 am

      Nice explanation

      Reply
    21. jj Gray on May 23, 2019 3:02 am

      how to display the am and pm of the time in your time code?

      Reply
      • amir on July 3, 2019 2:42 am

        var currentTime= new Date().toLocaleTimeString();

        Reply
    22. Hassan Tarique on May 20, 2019 9:42 am

      Very Helpful. Thanks!

      Reply
    23. imho on April 21, 2019 2:03 am

      thnx

      Reply
    24. Karthik on December 13, 2018 5:50 am

      This helped me. Thanks!

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Error: EACCES: permission denied, scandir (Resolved)
    • How To Install Python 3.11 on Ubuntu 22.04 / 20.04
    • How to Install Python 3.11 on Amazon Linux 2
    • An Introduction to the “./configure” Command: Compiling Source Code in Linux
    • How to Install PHP 8.x on Pop!_OS
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.