Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 按日期筛选-考虑用户';s时区_C#_Datetime_Timezone - Fatal编程技术网

C# 按日期筛选-考虑用户';s时区

C# 按日期筛选-考虑用户';s时区,c#,datetime,timezone,C#,Datetime,Timezone,我有一个帖子列表,其中帖子有一个“DateAdded”字段,该字段已设置为UTC时间。我需要过滤特定月份/年度的帖子。我正在努力解决的部分是如何考虑当前用户的时区(包括夏令时) 以下是需要考虑的变量: List<Post> posts = ...; TimeZoneInfo userTimeZone = ...; int year = ...; int month = ...; List posts=。。。; TimeZoneInfo用户时区=。。。; 整年=。。。; 整月=。。。

我有一个帖子列表,其中帖子有一个“DateAdded”字段,该字段已设置为UTC时间。我需要过滤特定月份/年度的帖子。我正在努力解决的部分是如何考虑当前用户的时区(包括夏令时)

以下是需要考虑的变量:

List<Post> posts = ...;
TimeZoneInfo userTimeZone = ...;
int year = ...;
int month = ...;
List posts=。。。;
TimeZoneInfo用户时区=。。。;
整年=。。。;
整月=。。。;
如果有人能告诉我正确的方法,我将不胜感激。谢谢

为什么不使用C#的
DateTime
类?它应该为您处理所有这些,并且它有一个比较功能

它有多个结构取决于你知道时间的准确程度

您可以使用LINQ对
列表进行过滤


编辑:对于时区转换,您只需将查询的
DateTime
值转换为UTC,然后进行筛选

// here are some posts
List<Post> posts = new List<Post>
{
    new Post {DateAdded = new DateTime(2013, 1, 1, 0, 0, 0, DateTimeKind.Utc)},
    new Post {DateAdded = new DateTime(2013, 2, 1, 0, 0, 0, DateTimeKind.Utc)},
    new Post {DateAdded = new DateTime(2013, 2, 2, 0, 0, 0, DateTimeKind.Utc)},
    new Post {DateAdded = new DateTime(2013, 3, 1, 0, 0, 0, DateTimeKind.Utc)},
    new Post {DateAdded = new DateTime(2013, 3, 2, 0, 0, 0, DateTimeKind.Utc)},
    new Post {DateAdded = new DateTime(2013, 3, 3, 0, 0, 0, DateTimeKind.Utc)},
};

// And the parameters you requested
TimeZoneInfo userTimeZone = TimeZoneInfo
                                .FindSystemTimeZoneById("Central Standard Time");
int year = 2013;
int month = 2;

// Let's get the start and end values in UTC.
DateTime startDate = new DateTime(year, month, 1);
DateTime startDateUtc = TimeZoneInfo.ConvertTimeToUtc(startDate, userTimeZone);
DateTime endDate = startDate.AddMonths(1);
DateTime endDateUtc = TimeZoneInfo.ConvertTimeToUtc(endDate, userTimeZone);

// Filter the posts to those values.  Uses an inclusive start and exclusive end.
var filteredPosts = posts.Where(x => x.DateAdded >= startDateUtc &&
                                     x.DateAdded < endDateUtc);
//下面是一些帖子
列表帖子=新列表
{
new Post{DateAdded=new DateTime(2013,1,1,0,0,0,DateTimeKind.Utc)},
new Post{DateAdded=new DateTime(2013,2,1,0,0,0,DateTimeKind.Utc)},
new Post{DateAdded=new DateTime(2013,2,2,0,0,0,DateTimeKind.Utc)},
new Post{DateAdded=new DateTime(2013,3,1,0,0,0,DateTimeKind.Utc)},
new Post{DateAdded=new DateTime(2013,3,2,0,0,0,DateTimeKind.Utc)},
new Post{DateAdded=new DateTime(2013,3,3,0,0,0,DateTimeKind.Utc)},
};
//以及您要求的参数
TimeZoneInfo用户时区=TimeZoneInfo
.FindSystemTimeZoneById(“中央标准时间”);
国际年=2013年;
整月=2;
//让我们获取UTC中的起始值和结束值。
DateTime startDate=新日期时间(年、月、1);
DateTime startDateUtc=TimeZoneInfo.ConvertTimeToUtc(startDate,userTimeZone);
DateTime endDate=startDate.AddMonths(1);
DateTime endDateUtc=TimeZoneInfo.ConvertTimeToUtc(endDate,userTimeZone);
//根据这些值筛选帖子。使用包含的开始和独占的结束。
var filteredPosts=posts.Where(x=>x.DateAdded>=startDateUtc&&
x、 添加日期<截止日期UTC);

这到底是如何解决OP关于时区的问题的?谢谢,但我已经知道很多了。我问的是怎么做,而不是我需要做什么。这就是我一直在寻找的。谢谢