小编Jer*_*emy的帖子

How to add an hour to a timespan. C#

I have my TimeSpan for a specific reason so it HAS to be in that format. I'm trying to add an hour on to the current time. Here is what I got, which does not work:

TimeSpan time1= TimeSpan.FromHours(1); // my attempt to add 2 hours
TimeSpan ts = DateTime.Now.TimeOfDay;
ts.Add(time1);
MessageBox.Show(ts.ToString()); // for showing me its result
Run Code Online (Sandbox Code Playgroud)

Can you please advise?

c#

18
推荐指数
4
解决办法
5万
查看次数

我怎样才能看到最接近一个的时间是我的列表?

我正在为我的手机开发一个应用程序,您可以在其中查看使用列表框输入时间的总线时间,然后检查列表,然后向您显示最接近它的时间或完全匹配.我已经尝试过自己并且运气好了我在这里发现的一些代码,但我仍然无法让它工作.

这里

    public  MainPage()
    {
        InitializeComponent();

        List<string> thedates = new List<string>();



        thedates.Add("0130");
        thedates.Add("0230");
        thedates.Add("0330");
        thedates.Add("0430");
        thedates.Add("0530");


        DateTime fileDate, closestDate;


        int min = int.MaxValue;

        foreach (DateTime date in theDates)
            if (Math.Abs(date.Ticks - fileDate.Ticks) < min)
            {
                min = date.Ticks - fileDate.Ticks;
                closestDate = date;
            }
    }
Run Code Online (Sandbox Code Playgroud)

错误:当前上下文中不存在名称"theDates".

对不起,如果这是简单或复杂的事情.任何帮助表示赞赏.

.net c# datetime

0
推荐指数
1
解决办法
145
查看次数

为什么我的DateTime不正确?

我想比较时间,我想用手机当前时间用于数学.现在我确定如果我能让这个字符串工作,那么我的应用程序将100%完成.

DateTime phonecurrentime =
    new DateTime(DateTime.Today.Day, DateTime.Now.Hour, DateTime.Now.Minute);
Run Code Online (Sandbox Code Playgroud)

唯一的问题是.... -

testbox.Text = phonecurrentime.ToString("dd hh:mm");
Run Code Online (Sandbox Code Playgroud)

显示错误的时间:28 12:00 - ????? 当天真的是30日,时间是01:30

如何让它显示正确的一天?

c# datetime

0
推荐指数
2
解决办法
1016
查看次数

标签 统计

c# ×3

datetime ×2

.net ×1