[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
date %格式化后的日期%&time %格式化后的时间%

TOP

回复 3# ygqiang

如果同步发生在凌晨,你不需要解决日期的更替问题?

TOP

  1. /*&cls
  2. @echo off
  3. set "netpath=%systemroot%\Microsoft.NET\Framework"
  4. for /f "delims=" %%a in ('dir /ad /b "%netpath%\v?.*"') do (
  5. if exist "%netpath%\%%a\csc.exe" (
  6.     set "cscpath=%netpath%\%%a\csc.exe"
  7.     goto :0
  8. )
  9. )
  10. echo;未安装.Net Framework 2.0及其上版本组件或相关程序丢失&pause&exit
  11. :0
  12. if not exist $SetDateTime.exe ("%cscpath%" /out:$SetDateTime.exe "%~f0")
  13. echo;
  14. echo;手动运行$SetDateTime.exe
  15. ping -n 4 0 >nul&exit
  16. */
  17. using System;
  18. using System.Net;
  19. using System.Net.Sockets;
  20. using System.Text;
  21. using System.Text.RegularExpressions;
  22. using System.Diagnostics;
  23. using System.Threading;
  24. class SetDateTime
  25. {
  26.     private static byte[] result = new byte[512];
  27.     static void Main(string[] args)
  28.     {
  29.         IPAddress ip = IPAddress.Parse(Dns.GetHostAddresses("time.nist.gov")[0].ToString());
  30.         Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  31.         try
  32.         {
  33.             clientSocket.Connect(new IPEndPoint(ip, 13));
  34.         }
  35.         catch
  36.         {
  37.             Console.WriteLine("连接服务器失败,程序将自动退出!");
  38.             Thread.Sleep(3000);
  39.             return;
  40.         }
  41.         int receiveLength = clientSocket.Receive(result);
  42.         Match dtstr = Regex.Match(Encoding.ASCII.GetString(result, 0 ,receiveLength), @"\d+(-\d+){2}\s\d+(:\d+){2}");
  43.         DateTime dt = DateTime.ParseExact(dtstr.ToString(), "yy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.CurrentCulture).AddHours(8);
  44.         Console.WriteLine("服务器当前日期时间为:{0}", dt.ToString());
  45.         Process proc = new Process();
  46.         proc.StartInfo.FileName = "cmd.exe";
  47.         proc.StartInfo.Arguments = "/c date "+dt.ToShortDateString().ToString()+"&time "+dt.ToLongTimeString().ToString();
  48.         proc.StartInfo.UseShellExecute = false;
  49.         proc.StartInfo.RedirectStandardError = true;
  50.         proc.Start();
  51.         string err = proc.StandardError.ReadToEnd();
  52.         Console.WriteLine(err != ""?err:"同步日期时间完成,请按任意键退出!");
  53.         Console.ReadKey(true);
  54.     }
  55. }
复制代码

TOP

回复 8# ygqiang

“手动”是告诉你批处理只是为了生成程序,以后直接运行程序即可

TOP

回复 10# ygqiang
Console.ReadKey(true);改为Thread.Sleep(3000);

TOP

返回列表