C#如何设置系统时间

jason176374 2011-03-16 11:33:26
有一个UTC格式的字符串,
然后要根据这个字符串来设置本地时间。

涉及到两个问题
1 UTC时间转换为本地时间
2 将转换后的本地时间设置为系统时间。

在网上查了查,发现只能用WIN32的API?
.net没有这样的函数来实现吗?
是否有例子参考,谢谢。
...全文
347 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
Daqing 2011-03-18
  • 打赏
  • 举报
回复
肯定会的!这个是datetime类型嘛!!! 我上面用的是local,“ DateTimeKind.Local”

你也可以用DateTimeKind.utc,小时减8
jason176374 2011-03-17
  • 打赏
  • 举报
回复
使用API的例子有吗?
特别是UTC转LOCAL的
jason176374 2011-03-17
  • 打赏
  • 举报
回复
t.hour = 12-8;
如果减8后得到负值,函数会自动调整日期,年月吗?
Daqing 2011-03-17
  • 打赏
  • 举报
回复

//api函数声明 
[dllimport ("kernel32.dll", charset=charset.ansi)]
public extern static bool setsystemtime(ref systemtime time);

private void button1_click(object sender, system.eventargs e)
{
//调用代码
systemtime t = new systemtime ();
t.year = 2000;
t.month = 1;
t.day = 2;
t.hour = 12-8; //这个函数使用的是0时区的时间,对于我们用+8时区的,时间要自己算一下.如要设12点,则为12-8
t.minute = 5;
bool v = setsystemtime(ref t);
console.writeline(v.tostring());
}

缭绕飘渺 2011-03-17
  • 打赏
  • 举报
回复
做过GPS对时的
读取时间然后跟系统时间比较
不同修改系统时间
代码给你个大概参考
using System.Runtime.InteropServices; //添加引用

//调用Kernel32.DLL
[DllImport("Kernel32.dll")]
public static extern void GetLocalTime(SystemTime st);
[DllImport("Kernel32.dll")]
public static extern void SetLocalTime(SystemTime st);

[StructLayout(LayoutKind.Sequential)]
public class SystemTime
{
public ushort wYear;
public ushort wMonth;
public ushort wDayOfWeek;
public ushort wDay;
public ushort Whour;
public ushort wMinute;
public ushort wSecond;
public ushort wMilliseconds;

}

SystemTime sSystemtime = new SystemTime();

dt = DateTime.ParseExact(sss, "HHmmssddMMyy", null);
dtSystem = DateTime.Now;
if (DateTime.Compare(dt, dtSystem) != 0)
{
sSystemtime.wDay = (ushort)dt.Day;
sSystemtime.wMonth = (ushort)dt.Month;
sSystemtime.wYear = (ushort)dt.Year;

sSystemtime.Whour = (ushort)dt.Hour;
sSystemtime.wMinute = (ushort)dt.Minute;
sSystemtime.wSecond = (ushort)dt.Second;

SetSystemTime(sSystemtime);//设置系统时间为读取到的时间

我就截取这么多
楼主应该能看明白
UTC转换过来应该不是问题吧
你查查MSDN就有了
Just4life 2011-03-17
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 tsapi 的回复:]
C# code

//引用
using System.Runtime.InteropServices;
//api函数声明
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool SetSystemTime(ref SYSTEMTIME time);
……
[/Quote]

+1
Daqing 2011-03-17
  • 打赏
  • 举报
回复
       
//引用
using System.Runtime.InteropServices;
//api函数声明
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool SetSystemTime(ref SYSTEMTIME time);
public struct SYSTEMTIME
{
public short Year;
public short Month;
public short DayOfWeek;
public short Day;
public short Hour;
public short Minute;
public short Second;
public short Milliseconds;
}
private void Setdatetime()
{
//调用代码
DateTime idag = new DateTime((int)DateTime.Now.AddYears(1).Year,//增加年
(int)DateTime.Now.AddMonths(0).Month,//增加月
(int)DateTime.Now.AddDays(0).Day,//增加天
(int)(DateTime.Now.AddHours(0).Hour),//增加小时
(int)(DateTime.Now.AddMinutes(0).Minute),//增加分
(int)(DateTime.Now.AddSeconds(0).Second),//增加秒
DateTimeKind.Local);
SYSTEMTIME s = new SYSTEMTIME();
s.Year = (short)idag.Year;
s.Month = (short)idag.Month;
s.DayOfWeek = (short)idag.DayOfWeek;
s.Day = (short)idag.Day;
s.Hour = (short)idag.Hour;
s.Minute = (short)idag.Minute;
s.Second = (short)idag.Second;
s.Milliseconds = (short)idag.Millisecond;
bool v= SetSystemTime(ref s);
Console.WriteLine(v.ToString());
}


//测试通过
mingcsharp 2011-03-17
  • 打赏
  • 举报
回复
使用API的例子有吗?
首先引用
using System.RunTime.Inter什么我忘了,只有这一个


DllImpor("")
public static void MessageBox(string a ,string b,int q ,int b);
类似这样的,很容易的你自已看下
机器人 2011-03-16
  • 打赏
  • 举报
回复
恩,只能用win32 api。。。
rekym 2011-03-16
  • 打赏
  • 举报
回复
只能用WIN32的API

C#本身没有这种函数

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧