C# 如何将两个字符串(日期和时间)组合成一个 DateTime

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14932950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:28:25  来源:igfitidea点击:

How to combine two strings (date and time) to a single DateTime

c#stringparsingdatetime

提问by user2082630

I have two strings:

我有两个字符串:

string one = "13/02/09";
string two = "2:35:10 PM";

I want to combine these two together and convert to a DateTime.

我想将这两者结合在一起并转换为DateTime.

I tried the following but it doesn't work:

我尝试了以下但它不起作用:

DateTime dt = Convert.ToDateTime(one + " " + two);
DateTime dt1 = DateTime.ParseExact(one + " " + two, "dd/MM/yy HH:mm:ss tt", CultureInfo.InvariantCulture);

What can I do to make this work?

我该怎么做才能使这项工作发挥作用?

采纳答案by Soner G?nül

Try like this;

像这样尝试;

string one = "13/02/09";
string two = "2:35:10 PM";

DateTime dt = Convert.ToDateTime(one + " " + two);
DateTime dt1 = DateTime.ParseExact(one + " " + two, "dd/MM/yy h:mm:ss tt", CultureInfo.InvariantCulture);

Console.WriteLine(dt1);

Here is a DEMO.

这是一个演示

HHusing a 24-hour clock from 00to 23. For example; 1:45:30 AM -> 01and 1:45:30 PM -> 13

HH使用 24 小时制从0023。例如; 1:45:30 AM -> 011:45:30 PM -> 13

husing a 12-hour clock from 1 to 12. For example; 1:45:30 AM -> 1and 1:45:30 PM -> 1

h使用从 1 到 12 的 12 小时制。例如;1:45:30 AM -> 11:45:30 PM -> 1

Check out for more information Custom Date and Time Format Strings

查看更多信息自定义日期和时间格式字符串

回答by Petar Ivanov

The problem is that the format string that you specify is not correct.

问题是您指定的格式字符串不正确。

'HH' means a dwo-digit hour, but you have a single digit hour.

'HH' 表示双位数的小时,但您有一位数的小时。

Use 'h' instead.

改用'h'。

So the full format is 'dd/MM/yy h:mm:ss tt'

所以完整格式是'dd/MM/yy h:mm:ss tt'

回答by Stephane Delcroix

use DateTime.Parse () to parse the date and the time separately. Then add the time component of the second one to the first one, like this

使用DateTime.Parse()分别解析日期和时间。然后把第二个的时间分量加到第一个上,像这样

var date = DateTime.Parse (one);
var time = DateTime.Parse (two);
var result = date + time - time.Date;

回答by Rawling

Your issue is with your hour specifier; you want h(The hour, using a 12-hour clock from 1 to 12), not HH(The hour, using a 24-hour clock from 00 to 23).

您的问题与您的小时说明符有关;您想要h小时,使用 1 到 12 的 12 小时制),而不是HH小时,使用 00 到 23 的 24 小时制)。

回答by Alex Filipovici

Try using a culture info which matches the DateTimeformat for your string values:

尝试使用与DateTime您的字符串值的格式匹配的文化信息:

DateTime dt = Convert.ToDateTime(one + " " + two,
    CultureInfo.GetCultureInfo("ro-RO"));

or modify the input string so that the hour has 2 digits:

或修改输入字符串,使小时有 2 位数字:

string one = "13/02/09";
string two = "02:35:10 PM";
DateTime dt1 = DateTime.ParseExact(one + " " + two, 
    "dd/MM/yy HH:mm:ss tt",
    CultureInfo.InvariantCulture);

回答by Ryszard D?egan

Use string two = "02:35:10 PM";instead of string two = "2:35:10 PM";and also hhinstead of HHdue to AM/PM format.

由于 AM/PM 格式而使用string two = "02:35:10 PM";代替string two = "2:35:10 PM";和也hh代替HH

Below is the code:

下面是代码:

string one = "13/02/09";
string two = "02:35:10 PM";

DateTime dateTime = DateTime.ParseExact(one + " " + two, "dd/MM/yy hh:mm:ss tt", CultureInfo.InvariantCulture);

回答by Kesty

The following code will do what you want. I used the UK culture to take care of the d/m/y structure of your date:

以下代码将执行您想要的操作。我用英国文化来处理你约会的 d/m/y 结构:

        string string1 = "13/2/09";
        string string2 = "2:35:10 PM";
        DateTime combined = DateTime.Parse(string1 + ' ' + string2, new CultureInfo("UK"));

回答by Chris S

Convert.ToDateTimeuses DateTime.ParseExactwith your current thread's culture, so you can make things a bit clearer by simply doing:

Convert.ToDateTime用途DateTime.ParseExact与当前线程的文化,这样你就可以让事情更清楚一点,只需做:

string date = "13/02/09";
string time = "2:35:10 PM";
DateTime dateTime = DateTime.Parse(date +" "+ time, new CultureInfo("en-GB"));
Console.WriteLine (dateTime);

That gives the result 13/02/2009 14:35:10, and forces the parse to use the en-GB date time formats. If your Windows installation is en-GB anyway, you don't need the CultureInfo(..)argument.

这给出了 result 13/02/2009 14:35:10,并强制解析使用 en-GB 日期时间格式。如果您的 Windows 安装是 en-GB,则不需要该CultureInfo(..)参数。

回答by Divya Agrawal

I had different format and the above answer did not work:

我有不同的格式,上面的答案不起作用:

string one = "2019-02-06";
string two = "18:30";

The solution for this format is:

这种格式的解决方案是:

DateTime newDateTime = Convert.ToDateTime(one).Add(TimeSpan.Parse(two));

The result will be: newDateTime{06-02-2019 18:30:00}

结果将是:newDateTime{06-02-2019 18:30:00}