CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: javascript and Computer date

  1. #1
    Join Date
    Oct 2016
    Posts
    3

    javascript and Computer date

    hello ,is there possible to write javascript so it can update date on computer... for example in windows there is in Control Panel>>Date and Time>> Internet time then "change settings" and there is a button "Update",so is there possible to click on "Update" using javascript? thank you

  2. #2
    Join Date
    Jun 2009
    Posts
    113

    Re: javascript and Computer date

    It depends on the focus of your question. If you want Javascript within the browser to update the date and time of the system accessing it (which could be a PC, tablet, phone etc) then you have no chance.
    On the other hand if you simply want a JavaScript run by an Administrator on a Windows PC to change the date and time, then this can be done. Here's an example which will show the current time and date and then change it:
    Code:
    var dtmNewDateTime = '20161031112209.673000+000';
    var strComputer = '.';
    var objWMIService = GetObject('winmgmts:{(Systemtime)}\\\\' + strComputer + '\\root\\cimv2');
    var colOSes = objWMIService.ExecQuery('Select * From Win32_OperatingSystem');
    
    for (var enumItems=new Enumerator( colOSes ); !enumItems.atEnd(); enumItems.moveNext()) 
    {
     var objOS = enumItems.item();
     WScript.Echo('Current time: '+objOS.LocalDateTime);
     objOS.SetDateTime(dtmNewDateTime);
    }
    this uses WMI which is worth reading about as it's a very powerful tool for system administrators to manage Windows PCs. https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx
    You save this to a file with a .JS extension and then you can go to a Administrator command prompt and run it using CScript. You probably won't have rights to double click on the file as this will try to run it with WScript and it won't auto-elevate.
    Another way to do this would be to run a command line program, e.g. DATE DD-MM-YYYY which can also be done within a JavaScript, but again only on Windows and only as an Administrator.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)