批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程
[批处理文件精品]批处理版照片整理器[批处理文件精品]纯批处理备份&还原驱动在线第三方下载
返回列表 发帖

[原创代码] JS 同步本机时间与网络时间

  1. //文件名称:SyncNetTime.js
  2. //功能说明:同步本机时间与网络时间
  3. //使用方法:Cscript.exe //nologo SyncNetTime.js
  4. //测试环境:系统 Win10 x64 时间 17/2/10 23:35 用户 Yu2n
  5. //以管理员运行
  6. function GetSystemVersion() {
  7. var os = GetObject("winmgmts:").InstancesOf("Win32_OperatingSystem");
  8. for (var e = new Enumerator(os); ! e.atEnd(); e.moveNext()) {
  9. var v = e.item().Version;
  10. var ss = v.split('.');
  11. return ss[0] + ss[1];
  12. }
  13. return - 1;
  14. }
  15. if (GetSystemVersion() >= 60) {
  16. var cmd = WScript.ScriptFullName;
  17. if (cmd.substring(cmd.length - 3) != ".jS") {
  18. var Shell = new ActiveXObject("Shell.Application");
  19. Shell.ShellExecute("wscript.exe", "\"" + cmd.substring(0, cmd.length - 3) + ".jS\"", "", "runas", 1);
  20. WScript.Quit(0);
  21. }
  22. }
  23. //获取网络时间,从 www.beijing-time.org 页面
  24. var getNetDate = function() {
  25. var s='';
  26. try{
  27. var http = new ActiveXObject("Microsoft.XMLHTTP");
  28. http.open("GET", "http://www.beijing-time.org/time15.asp?rnd=" + (new Date()), false);
  29. http.send();
  30. s = http.responseText;
  31. }catch(e){};
  32. if (/^t0\=[\s\S]+nyear\=[\s\S]+nsec\=\d+\;$/igm.test(s) == true) {
  33. eval(s);
  34. return new Date(nyear+'/'+nmonth+'/'+nday+' '+nhrs+':'+nmin+':'+nsec);
  35. } else {
  36. WScript.Echo("警告:获取网络时间失败!")
  37. WScript.Quit(0);
  38. }
  39. };
  40. //设置时间
  41. function ChangeDate()
  42. {
  43. var WmiService, ComputerName, OSList, OSEnum, OS, DateTime;
  44. ComputerName = ".";
  45. WmiService = GetObject ("winmgmts:{impersonationLevel=impersonate, (Systemtime)}!//" + ComputerName + "/root/cimv2");
  46. OSList = WmiService.InstancesOf ("Win32_OperatingSystem");
  47. DateTime = new ActiveXObject ("WbemScripting.SWbemDateTime");
  48. OSEnum = new Enumerator (OSList);
  49. for ( ; !OSEnum.atEnd(); OSEnum.moveNext())
  50. {
  51. OS = OSEnum.item();
  52. var dtNewDate = getNetDate(); //获取网络时间
  53. DateTime.Value = OS.LocalDateTime;
  54. DateTime.Year = dtNewDate.getFullYear();
  55. DateTime.Month = dtNewDate.getMonth()+1;
  56. DateTime.Day = dtNewDate.getDate();
  57. DateTime.Hours = dtNewDate.getHours();
  58. DateTime.Minutes = dtNewDate.getMinutes();
  59. DateTime.Seconds = dtNewDate.getSeconds();
  60. if (OS.SetDateTime(DateTime.Value) != 0)
  61. WScript.Echo("警告:设置系统时间失败!");
  62. else
  63. WScript.Echo("提示:设置成功。当前时间:" + new Date(DateTime.GetVarDate()).toLocaleString());
  64. }
  65. }
  66. ChangeDate();
  67. WScript.Quit(0);
复制代码
『千江有水千江月』千江有水,月映千江;万里无云,万里青天。    http://yu2n.qiniudn.com/

本帖最后由 yu2n 于 2017-2-13 16:30 编辑

更新内容:修复对网络内容使用 eval 的安全问题。
  1. //文件名称:SyncNetTime.js
  2. //功能说明:同步本机时间与网络时间
  3. //使用方法:Cscript.exe //nologo SyncNetTime.js
  4. //测试环境:系统 Win10 x64 时间 17/2/12 17:00 用户 Yu2n
  5. //更新内容:Fix 获取网络时间,防止 eval 安全问题
  6. //以管理员运行
  7. function GetSystemVersion() {
  8. var os = GetObject("winmgmts:").InstancesOf("Win32_OperatingSystem");
  9. for (var e = new Enumerator(os); ! e.atEnd(); e.moveNext()) {
  10. var v = e.item().Version;
  11. var ss = v.split('.');
  12. return ss[0] + ss[1];
  13. }
  14. return - 1;
  15. }
  16. if (GetSystemVersion() >= 60) {
  17. var cmd = WScript.ScriptFullName;
  18. if (cmd.substring(cmd.length - 3) != ".jS") {
  19. var Shell = new ActiveXObject("Shell.Application");
  20. Shell.ShellExecute("wscript.exe", "\"" + cmd.substring(0, cmd.length - 3) + ".jS\"", "", "runas", 1);
  21. WScript.Quit(0);
  22. }
  23. }
  24. //获取网络时间,从 www.beijing-time.org 页面,使用正则验证结果,防止 eval 安全问题
  25. var getNetDate = function() {
  26. var s='';
  27. try{
  28. var http = new ActiveXObject("Microsoft.XMLHTTP");
  29. http.open("GET", "http://www.beijing-time.org/time15.asp?rnd=" + (new Date()), false);
  30. http.send();
  31. s = http.responseText;
  32. }catch(e){};
  33. var re = /^[\s\S]*(nyear\=\d+;[\r\n]+)(nmonth\=\d+;[\r\n]+)(nday\=\d+;[\r\n]+)(nwday\=\d+;[\r\n]+)(nhrs\=\d+;[\r\n]+)(nmin\=\d+;[\r\n]+)(nsec\=\d+;)[\s\S]*$/igm;
  34. if (re.test(s) == true) {
  35. eval(s.replace(re,'$1$2$3$5$6$7')); //使用正则替换结果,防止 eval 安全问题
  36. return new Date(nyear+'/'+nmonth+'/'+nday+' '+nhrs+':'+nmin+':'+nsec);
  37. } else {
  38. WScript.Echo("警告:获取网络时间失败!")
  39. WScript.Quit(0);
  40. };
  41. };
  42. //设置时间
  43. function ChangeDate()
  44. {
  45. var WmiService, ComputerName, OSList, OSEnum, OS, DateTime;
  46. ComputerName = ".";
  47. WmiService = GetObject ("winmgmts:{impersonationLevel=impersonate, (Systemtime)}!//" + ComputerName + "/root/cimv2");
  48. OSList = WmiService.InstancesOf ("Win32_OperatingSystem");
  49. DateTime = new ActiveXObject ("WbemScripting.SWbemDateTime");
  50. OSEnum = new Enumerator (OSList);
  51. for ( ; !OSEnum.atEnd(); OSEnum.moveNext())
  52. {
  53. OS = OSEnum.item();
  54. var dtNewDate = getNetDate(); //获取网络时间
  55. DateTime.Value = OS.LocalDateTime;
  56. DateTime.Year = dtNewDate.getFullYear();
  57. DateTime.Month = dtNewDate.getMonth() + 1;
  58. DateTime.Day = dtNewDate.getDate();
  59. DateTime.Hours = dtNewDate.getHours();
  60. DateTime.Minutes = dtNewDate.getMinutes();
  61. DateTime.Seconds = dtNewDate.getSeconds();
  62. if (OS.SetDateTime(DateTime.Value) != 0) {
  63. WScript.Echo("警告:设置系统时间失败!");
  64. } else {
  65. WScript.Echo("提示:设置成功。当前时间:" + new Date(DateTime.GetVarDate()).toLocaleString());
  66. };
  67. }
  68. }
  69. ChangeDate();
  70. WScript.Quit(0);
复制代码
1

评分人数

『千江有水千江月』千江有水,月映千江;万里无云,万里青天。    http://yu2n.qiniudn.com/

TOP

回复 2# yu2n
感谢分享代码,让我学到很多js技巧。外国人的月一直都是加1     getMonth()+1

TOP

返回列表