如何在c#中设置日期时间x小时。 [英] how to set datetime x hours back in c#.

查看:102
本文介绍了如何在c#中设置日期时间x小时。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友....我正在开发一个将在世界范围内使用的网络应用程序...主要在沙特阿拉伯和印度。沙特阿拉伯的时间比印度早2:30。假设从印度发现的任何点击比印度的日期时间都将保存在数据库中...我们如何在沙特阿拉伯展示它。并且必须转换对应于阿拉伯日期时间的数据库日期。





我在互联网上搜索...但是直到找不到合适的解决方案....所以请指导。我怎么能做到这一点。提前谢谢。

解决方案

 DateTime dt =  new  DateTime() ; 
dt = DateTime.Now;
DateTime newdt = new DateTime();
TimeSpan tim = new TimeSpan(-X, 0 0 0 0 ); // hours Timespan(0,-X,0,0,0)
newdt = dt.Add(tim);
MessageBox.Show(newdt.ToString());







x =你的号码( + x表示未来日期,-X表示过去日期时间)

希望此帮助完整。



Timespan(-x,0,0, 0,0)将指定过去的日期和时间

但是我刚刚仔细阅读了你的要求时间吗?

所以只需要这个Timespan(0,-X, 0,0,0)


hello friends.... im working on a web application which will be used world wide ... mainly in saudi arabia and india. the timing of saudi arabia is 2:30 back compare to india. suppose any click found from india than india's datetime will be saved in database ... how we have to show it in saudi arabia. and have to convert database date corresponding to arabia datetime.


im serching on the internet ... but didnt find a proper solution till yet.... so please guide . how i can do this . thanks in advance .

解决方案

DateTime dt = new DateTime();
           dt = DateTime.Now;
           DateTime newdt = new DateTime();
           TimeSpan tim = new TimeSpan(-X, 0, 0, 0, 0);//hours Timespan(0,-X,0,0,0)
           newdt = dt.Add(tim);
           MessageBox.Show(newdt.ToString());




x = your number(+x means future date, -X means past datetime)
hope this help full.

Timespan(-x,0,0,0,0) will specify date and time of past
but just noew i read carefully your requirment hours right??
so just tri this Timespan(0,-X,0,0,0)


这篇关于如何在c#中设置日期时间x小时。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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