C#解析日期和时间 [英] C# Parsing Dates and Times

查看:280
本文介绍了C#解析日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序一起

DateTime activityDate = DateTime.Parse(tempDate + " " + tempTime);



其中的 tempDate 的是价值观,如2009-12-字符串01(即YYYY-MM-DD)
和的 tempTime 的是值,如字符串二十三时12分10秒(即HH:MM:SS)

Where tempDate is a string with values such as "2009-12-01" ( i.e. yyyy-mm-dd ) and tempTime is a string with values such as "23:12:10" ( i.e. hh:mm:ss )

首先,有没有更好的方式来组合这些得到一个日期时间,二是超过安全代码的任何地区工作(如果不是有没有办法来处理这个)

Firstly, is there a better way to combine these to get a DateTime, and secondly is the code above safe to work in any region ( if not is there a way to handle this )

在嗯日更密切关注的连接的日期和放大器;时间实际上是这种格式2009-11-26T19:37:56 + 00:00? - 什么是格式字符串的日期/时间的时区的部分。

Hmm looking at the date more closely the concatenated date & time is actually in this format "2009-11-26T19:37:56+00:00" - what's the format string for the timezone part of the date/time?

推荐答案

如果格式有保证, ParseExact 可能会更安全(sepcifying模式):

If the format is guaranteed, ParseExact may be safer (sepcifying the pattern):

DateTime activityDate = DateTime.ParseExact(tempDate + " " + tempTime,
    "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);

这篇关于C#解析日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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