如何为给定日期添加时间整数 [英] How do I add the time for a given date the time is in integer
本文介绍了如何为给定日期添加时间整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何计算到某个日期的价值
time格式应为整数格式
例子
日期:20-10-2016
时间:1800
必填格式20-10-2016 18:00:00:000
我尝试了什么:
我尝试过使用时间跨度和datatime.addhours请帮忙
How to time value to a date
time Format shall be in the interger format
Example
Date : 20-10-2016
Time : 1800
Required Format 20-10-2016 18:00:00:000
What I have tried:
I have tried using time span and datatime.addhours please help
推荐答案
这取决于你在处理什么:
1)如果你有一个字符串(或两个字符串)然后使用TryParseExact:
It depends on what you are dealing with:
1) If you have a string (or two strings) then use TryParseExact:
string date = "20-10-2016";
string time = "1800";
DateTime dt;
if (DateTime.TryParseExact(date + time, "dd-MM-yyyyHHmm", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
{
...
}
2)如果你已经有了DateTime值,请使用AddHours:
2) If you have a DateTime value already, use AddHours:
DateTime dt = new DateTime(2016, 10, 20);
dt = dt.AddHours(18);
首先需要解析时间价值:
First you need to parse the time value:
var time = 1800;
var hours = time / 100;
var minutes = time % 100;
接下来设置您日期的值:
Next set the values on your date:
var mydate = yourdate;
mydate.Hour = hours;
mydate.Minute = minutes;
你也可以这样做:
You could also do this:
// assuming you already have a date and a time...
DateTime date = new DateTime(2016, 11, 8);
int time = 1800;
// you could do this
DateTime newDate2 = new DateTime(date.Year,
date.Month,
date.Day,
(int)Math.Floor(time * 0.01),
time - ((int)Math.Floor(time * 0.01) * 100),
0,
0);
这篇关于如何为给定日期添加时间整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文