locked
Get Current Date & Time (should not change after change local system Time/Date) RRS feed

  • Question

  • I search a lot but unable to find solution of my problem, 

    Requirement: To Get Current Date & Time.

    Issue: While changing Date or timezone or time in Date time setting of the laptop its start fetching local timezone.

    I want UTC +05:30 ie. Asia/Kolkata time Zone

    Note : It should work in IE also.

    Best Regards


    Shiv Sharma

    Thursday, July 16, 2020 6:33 PM

All replies

  • Hi,

    You could try the following method:

    function dateFormateFun(date) {
            var year = date.getUTCFullYear();
            var day = date.getUTCDate();
            var month = date.getUTCMonth() + 1;
            var hour = date.getUTCHours() + 5;//UTC +5:30 +5   UTC-5:30 -5
            var min = date.getUTCMinutes() +30;//UTC +5:30 +30   UTC-5:30 -30
    var dateStr;
            if (hour <= 0) {
                day = day - 1;
                hour = 23;
            }
            if (day <= 0) {
                month = month - 1;
                if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
                    day = 31;
                } else if (month == 4 || month == 6 || month == 9 || month == 11) {
                    day = 30;
                } else {
                    if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
                        day = 29;
                    } else {
                        day = 28;
                    }
                }
            }
            if (month <= 0) {
                year = year - 1;
                month = 12;
            }
            if (min < 10) {
                min = "0" + min;
            }
            dateStr = "" + month + "/" + day + "/" + year + " " + hour + ":" + min;
    return dateStr;
        }

    Tip:the method return a string,you could change it to styles you want.And if you want to change to other time zone,you need to modify code as the example in code.

    Test result in IE:

    This “SharePoint 2013 - Development and Programming” Forum will be migrating to a new home on Microsoft Q&A, please refer to this sticky post for more details.

    Best Regadrs,

    Amos


    "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 !

    Friday, July 17, 2020 8:26 AM
  • Its not working , When i change time of the laptop its picking that Changed time :( 

     unable to find any solution . Many people suggest many solution but none is working .


    Shiv Sharma

    Friday, July 17, 2020 10:25 AM
  • Hi,

    In order not to be affected by local time,we need to get Kolkata time online instead of converting through local time.

    Test code:

    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    
    <script type="text/javascript">
    $(document).ready(function() {
        loadReportData();
    })
    
    function loadReportData(){
      $.ajax({
        url: "http://worldtimeapi.org/api/timezone/Asia/Kolkata",
        type: "GET",
        headers: {
            "Accept":"application/json;odata=verbose"   
        },
        success:dosomething,
        error:dosomethingelse
      })    
    
    }
    
    function dosomething(data){
     console.log("dosomething");
     console.log(data.datetime);
    
    }
    function dosomethingelse(data){
     console.log("error");
    
    }
    </script>

    Test result:

    Best Regards,

    Amos


    "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 7:44 AM
  • Hi,

    I am checking to see how things are going there on this issue. Please let us know if you would like further assistance.

    You can mark the helpful post as answer to help other community members find the helpful information quickly.
    If you need further help, it would be better to provide details requirement(demo screenshot) so we could try to research.

    Best Regards,

    Amos


    "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 !

    Thursday, July 23, 2020 8:15 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:51 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:59 AM
  • Hi,

    The type of data.datetime is string.In order to avoid being affected by the Date() object, I think we could use some methods of string to splice the desired time format.

    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    
    <script type="text/javascript">
    $(document).ready(function() {
        loadReportData();
    })
    
    function loadReportData(){
      $.ajax({
        url: "http://worldtimeapi.org/api/timezone/Asia/Kolkata",
        type: "GET",
        headers: {
            "Accept":"application/json;odata=verbose"   
        },
        success:dosomething,
        error:dosomethingelse
      })    
    
    }
    
    function dosomething(data){
     
     console.log(data.datetime);
    var ymd=data.datetime.substring(0,10).split('-');
    var time=data.datetime.substring(11,19);
    var KolkataTime=ymd[1]+"/"+ymd[2]+"/"+ymd[0]+" "+time;
    console.log(KolkataTime);
    }
    function dosomethingelse(data){
     console.log("err0r");
    
    
    }
    </script>

    Test result:

    Best Regards,

    Amos


    "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 !

    Friday, July 24, 2020 6:05 AM
  • Hi,

    Would you please provide us with an update on the status of your issue?

    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,

    Amos


    "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, August 3, 2020 8:00 AM