locked
Jquery Get Current Time/Date RRS feed

  • Question

  • 1-Get Current Date  Format yyyy/mm/dd

    2-Get Current Time Format HH:mm:ss

    Issue: When anyone change time from laptop date & time setting. it is fetching system time not actual current time.

    Timezone- Asia/Kolkata.

    Note: I also tried Timezone but when user changes the time from date and time setting. then function time also start picking system time.Also it must work in IE.


    Shiv Sharma

    Saturday, July 18, 2020 7:00 AM

All replies

  • You could reach out to a time server/API (see below). Regarding the formatting, there are many options in the JavaScript Date() method for formatting (see more here: https://codehandbook.org/javascript-date-format/)

    <HTML>
    <body>
    <script type="text/javascript">
    var url = "http://worldclockapi.com/api/json/utc/now"
    function GetTimeViaAPI() {
        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function() {
             if (this.readyState == 4 && this.status == 200) {
                 var result = JSON.parse(this.responseText);
                 console.log(Date(Date.parse(result.currentDateTime)));      
             }
        };
        xhttp.open("GET", url, true);
        xhttp.send();
    }
    
    GetTimeViaAPI();
    </script>
    </body>
    </HTML>
    

    Saturday, July 18, 2020 11:05 AM
  • Hi SharePoint User02,

    JavaScript Date objects represent a date based on system settings. If the time in your pc is not correct, you can request a network date time as @Robotitude suggested.

    Best Regards,

    Baker Kong


    "SharePoint" forums will be migrating to a new home on Microsoft Q&A !
    We invite you to post new questions in the "SharePoint" forums' new home on Microsoft Q&A !

    Monday, July 20, 2020 2:27 AM
  • I am having the issue with the format what you are suggested .

    I need separate date & time in below format 

    Date: MM/DD/YYYY

    Time: HH:mm (24 hours) 


    Shiv Sharma

    Friday, July 24, 2020 4:55 AM
  • I am having the issue with the format what you are suggested .

    I need separate date & time in below format 

    Date: MM/DD/YYYY

    Time: HH:mm (24 hours) 


    Shiv Sharma

    Friday, July 24, 2020 5:00 AM
  • The components of the Date() object can be extracted like this:

    let current_datetime = Date(Date.parse(result.currentDateTime));
    
    Console.log(current_datetime);
    
    let date_format_a =
    (current_datetime.getMonth() + 1) + "/" +
    current_datetime.getDate() + "/" + current_datetime.getFullYear();
    
    Console.log(date_format_a);
    
    let date_format_b = current_datetime.getHours() + ":" + current_datetime.getMinutes();
    
    Console.log(date_format_b);

    Friday, July 24, 2020 2:56 PM
  • Hi SharePoint User02,

    We have not heard from you for a long time. We'd like to notify you that This forum will be locked down, if you still have issues about this question, you can continue to post in here before 8/10/2020. It is recommended to create a new question on Microsoft Q&A, we will continue to support you on the new home.

    Best Regards,

    Baker Kong


    "SharePoint" forums will be migrating to a new home on Microsoft Q&A !
    We invite you to post new questions in the "SharePoint" forums' new home on Microsoft Q&A !

    Wednesday, August 5, 2020 2:48 AM