有问题,我的转换日期时间为UTC [英] Having problems with converting my DateTime to UTC

查看:244
本文介绍了有问题,我的转换日期时间为UTC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我储存我的UTC格式的所有日期在我的数据库。我要求他们的时区的用户,我想用自己的时区,再加上我猜是服务器的时间来找出UTC他们。

在我有我想要做一个搜索,看看有什么范围内使用其新转换的UTC日期的数据库。

但我总是得到这个例外。

  System.ArgumentException是未处理由用户code
消息=转换无法完成,因为
提供的日期时间没有Kind属性设置正确。
例如,当Kind属性是DateTimeKind.Local,
源时区必须TimeZoneInfo.Local。
参数名:sourceTimeZone
 

我不知道为什么我收到此。

我试过2种方法

 的TimeZoneInfo区= TimeZoneInfo.FindSystemTimeZoneById(ID);
 //我也试过DateTime.UtcNow
 现在的DateTime = DateTime.SpecifyKind(DateTime.Now,DateTimeKind.Local);
 VAR UTC = TimeZoneInfo.ConvertTimeToUtc(目前,区);
 

这没有让我累了。

 的DateTime现在= DateTime.SpecifyKind(DateTime.Now,DateTimeKind.Local);
 VAR UTC = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(现在,
                                           了zoneid,TimeZoneInfo.Utc.Id);
 

这也没有都具有相同的错误。我究竟做错了什么?

编辑将这项工作?

 日期时间localServerTime = DateTime.SpecifyKind(DateTime.Now,DateTimeKind.Local);
 的TimeZoneInfo信息= TimeZoneInfo.FindSystemTimeZoneById(ID);

 VAR usersTime = TimeZoneInfo.ConvertTime(localServerTime,资讯);

 VAR UTC = TimeZoneInfo.ConvertTimeToUtc(usersTime,用户信息);
 

编辑2 @乔恩斯基特

雅,我只是在想,我可能甚至不需要做这一切。时间的东西迷我现在所以这就是为什么该职位可能不明确的,因为它应该是。我从来不知道到底DateTime.Now越来越(我试图改变我的时区到另一个时区,它不停地让我的本地时间)。

这是我想要我的东西做的。用户来到网站增加了一些警惕,它现在被保存为UTC(以前是DateTime.Now后来​​有人建议存储一切UTC)。

所以,之前用户会来我的网站,并根据我在哪里托管服务器是它可以像在第二天。因此,如果警报被说成是显示在8月30日(其时间),但与服务器的时间差,他们能来8月29日和警报将被示出。

所以我想打击的。所以,现在我不知道我应该只储存那么它们的本地时间使用此偏移的东西?或只是存储UTC时间。随着只是存储UTC时间仍可能是错误的,因为用户仍然可能会思考本地时间,我不知道如何UTC真的工作它仍可能最终的时间差。

EDIT3

  VAR信息= TimeZoneInfo.FindSystemTimeZoneById(ID)

 的DateTimeOffset usersTime = TimeZoneInfo.ConvertTime(DataBaseUTCDate,
                                             TimeZoneInfo.Utc,资讯);
 

解决方案

的DateTime 结构支持只有两个时区:

  • 本地时区机器在运行。
  • 和UTC。

有一个看的DateTimeOffset 的结构。

  VAR信息= TimeZoneInfo.FindSystemTimeZoneById(东京标准时间);

的DateTimeOffset localServerTime = DateTimeOffset.Now;

的DateTimeOffset usersTime = TimeZoneInfo.ConvertTime(localServerTime,资讯);

的DateTimeOffset UTC = localServerTime.ToUniversalTime();

Console.WriteLine(本地时间:{0},localServerTime);
Console.WriteLine(用户的时间:{0},usersTime);
Console.WriteLine(UTC:{0},UTC);
 

输出:

 当地时间:2009年8月30号20点48分17秒+02:00
使用时间:2009年8月31日3时48分17秒+09:00
UTC:二零零九年八月三十〇日18点48分17秒+00:00
 

I am storing all my dates in UTC format in my database. I ask the user for their timezone and I want to use their time zone plus what I am guessing is the server time to figure out the UTC for them.

Once I have that I want to do a search to see what is range in the database using their newly converted UTC date.

but I always get this exception.

System.ArgumentException was unhandled by user code  
Message="The conversion could not be completed because the   
supplied DateTime did not have the Kind property set correctly.  
For example, when the Kind property is DateTimeKind.Local,   
the source time zone must be TimeZoneInfo.Local.  
Parameter name: sourceTimeZone"

I don't know why I am getting this.

I tried 2 ways

 TimeZoneInfo zone = TimeZoneInfo.FindSystemTimeZoneById(id);
 // I also tried DateTime.UtcNow
 DateTime now = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Local); 
 var utc = TimeZoneInfo.ConvertTimeToUtc(now , zone );

This failed so I tired

 DateTime now = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Local); 
 var utc = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(now, 
                                           ZoneId, TimeZoneInfo.Utc.Id);

This also failed both with the same error. What am I doing wrong?

Edit would this work?

 DateTime localServerTime = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Local);
 TimeZoneInfo info = TimeZoneInfo.FindSystemTimeZoneById(id);

 var usersTime = TimeZoneInfo.ConvertTime(localServerTime, info);

 var utc = TimeZoneInfo.ConvertTimeToUtc(usersTime, userInfo);

Edit 2 @ Jon Skeet

Ya I was just thinking about that I might not even need to do all this. Time stuff confuses me right now so thats why the post may not be as clear as it should be. I never know what the heck DateTime.Now is getting (I tried to change my Timezone to another timezone and it kept getting my local time).

This is what I wanted my stuff to do. User comes to site adds some alert and it gets now saved as utc (before it was DateTime.Now then someone suggested to store everything UTC).

So before a user would come to my site and depending where my hosting server was it could be like on the next day. So if the alert was said to be shown on August 30th (their time) but with the time difference of the server they could come on August 29th and the alert would be shown.

So I wanted to combat that. So now I am not sure should I just store their local time then use this offset stuff? Or just store UTC time. With just storing UTC time still might be wrong since the user still probably would be thinking in local time and I am not sure how UTC really works it still could end up a difference of time.

Edit3

 var info = TimeZoneInfo.FindSystemTimeZoneById(id)

 DateTimeOffset usersTime = TimeZoneInfo.ConvertTime(DataBaseUTCDate,
                                             TimeZoneInfo.Utc, info);

解决方案

The DateTime structure supports only two timezones:

  • The local timezone the machine is running in.
  • and UTC.

Have a look at the DateTimeOffset structure.

var info = TimeZoneInfo.FindSystemTimeZoneById("Tokyo Standard Time");

DateTimeOffset localServerTime = DateTimeOffset.Now;

DateTimeOffset usersTime = TimeZoneInfo.ConvertTime(localServerTime, info);

DateTimeOffset utc = localServerTime.ToUniversalTime();

Console.WriteLine("Local Time:  {0}", localServerTime);
Console.WriteLine("User's Time: {0}", usersTime);
Console.WriteLine("UTC:         {0}", utc);

Output:

Local Time:  30.08.2009 20:48:17 +02:00
User's Time: 31.08.2009 03:48:17 +09:00
UTC:         30.08.2009 18:48:17 +00:00

这篇关于有问题,我的转换日期时间为UTC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆