如何两个字符串(日期和时间)结合到单一的DateTime [英] How to combine two strings (date and time) to a single DateTime

查看:136
本文介绍了如何两个字符串(日期和时间)结合到单一的DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个字符串:

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



我想这两个结合在一起,并转换为的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?

推荐答案

尝试这样;

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.

HH 的使用24小时时钟从 00 23 。例如; 上午1时45分三十秒 - > 01 下午1时45分30秒 - > 13

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

^ h 的使用12小时时钟从1到12。例如; 上午1时45分三十秒 - > 1 下午1时45分三十零秒 - > 1

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

检查出更多的信息。自定义日期和时间格式字符串

Check out for more information Custom Date and Time Format Strings

这篇关于如何两个字符串(日期和时间)结合到单一的DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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