本文介绍了获取过去和未来的datetimeoffset的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用offset属性获取过去2天和未来2天的记录。如果我过了2天我可以使用我的偏移属性这样的字符串偏移= -2,但如果我想使用过去和未来,如何计算?
提前谢谢。
Get past 2days and future 2days records using the offset property. If i get past 2day i can use my offset property something like this "string offset = -2", but if i want to use both past and future, how to calculate?
Thanks in advance.
推荐答案
DateTime now = Datetime.Now;
DateTime 2daysAgo = now.AddDays(-2);
DateTime 2daysLater = now.AddDays(2);
int offset = 2 ;
DateTime past = DateTime.Now.AddDays(-offset);
DateTime future = DateTime.Now.AddDays(offset);
// required
using System.Linq;
// generate range: -2,-1,0,1,2
private IEnumerable<int> FourDays = Enumerable.Range(-2, 5);
// somewhere in a method
DateTime rightNow = DateTime.Now;
// test ...
foreach (var i in FourDays)
{
// test ...
Console.WriteLine(rightNow.AddDays(i));
// call method to get Records
// ...
}</int>
这篇关于获取过去和未来的datetimeoffset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!