将日期字符串转换为DateTime。请举例 [英] Converting a date string to a DateTime. Example please

查看:69
本文介绍了将日期字符串转换为DateTime。请举例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个格式为DD-MM-YY hh:mm:ss的简单字符串,我需要将
转换为DateTime值。

我知道这是一个标准问题,但请不要只链接到关于Parse()和ParseExact()的所有

MSDN页面。

我''我一直在那里阅读有关使用IFormatProviders的所有信息,

DateTimeStyles,CultureInfo .......


底线就是我可以''让它工作,所以有人可以请

发布5-10行工作代码来执行这个简单的

转换吗?

谢谢,

Kim

I have a simple string in the format "DD-MM-YY hh:mm:ss", that I need to
convert to a DateTime value.
I know this is a standard problem, but please don''t just link to all the
MSDN pages regarding Parse() and ParseExact().
I''ve been there and read all the information about using IFormatProviders,
DateTimeStyles, CultureInfo.......

Bottom line is that I can''t get it working, so could someone please just
post 5-10 lines of working code that is needed to perform this simple
conversion?

Thank You,
Kim

推荐答案

" Kim Hellan" <所以***** @ nowhere.com>在留言中写道

news:ec ************** @ TK2MSFTNGP12.phx.gbl ...
"Kim Hellan" <so*****@nowhere.com> wrote in message
news:ec**************@TK2MSFTNGP12.phx.gbl...
I有一个格式为DD-MM-YY hh:mm:ss的简单字符串,我需要转换为DateTime值。


没问题。

我知道这是一个标准问题,但请不要只链接到有关Parse的所有
MSDN页面()和ParseExact()。


呃,这就是答案的所在......

我去过那里并阅读有关使用IFormatProviders的所有信息,
DateTimeStyles,CultureInfo .......


真的吗?也许你有一个糟糕的一天...

底线是我不能让它工作,所以有人可以请
发布5-10行工作代码,这是需要执行这个简单的转换吗?
I have a simple string in the format "DD-MM-YY hh:mm:ss", that I need to
convert to a DateTime value.
No problem.
I know this is a standard problem, but please don''t just link to all the
MSDN pages regarding Parse() and ParseExact().
Er, well that''s where the answer lies...
I''ve been there and read all the information about using IFormatProviders,
DateTimeStyles, CultureInfo.......
Really? Maybe you were having a bad day...
Bottom line is that I can''t get it working, so could someone please just
post 5-10 lines of working code that is needed to perform this simple
conversion?




string strDate =" 13-10-05 10:19:26" ;;

DateTime dtmDate = DateTime.Parse(strDate);



string strDate = "13-10-05 10:19:26";
DateTime dtmDate = DateTime.Parse(strDate);


>>我有一个格式为DD-MM-YY hh:mm的简单字符串: ss",我需要
>>I have a simple string in the format "DD-MM-YY hh:mm:ss", that I need to
转换为DateTime值。
convert to a DateTime value.



没问题。



No problem.

我知道这是一个标准问题,但请不要只链接到有关Parse()和ParseExact()的所有
MSDN页面。
I know this is a standard problem, but please don''t just link to all the
MSDN pages regarding Parse() and ParseExact().




Er, well that''s where the answer lies...

我去过那里阅读有关使用的所有信息
IFormatProviders,DateTimeStyles,CultureInfo 。 ......
I''ve been there and read all the information about using
IFormatProviders, DateTimeStyles, CultureInfo.......



真的吗?也许你有一个糟糕的一天...



Really? Maybe you were having a bad day...

底线是我不能让它工作,所以有人可以请你只需发布5-10行执行这个简单的转换所需的工作代码?
Bottom line is that I can''t get it working, so could someone please just
post 5-10 lines of working code that is needed to perform this simple
conversion?



string strDate =" 13-10-05 10:19:26" ;;
DateTime dtmDate = DateTime.Parse(strDate);



string strDate = "13-10-05 10:19:26";
DateTime dtmDate = DateTime.Parse(strDate);




好​​的,我们可以说日期字符串是:07-11-05 10:19:26。

上面简单的Parse()方法如何知道什么是年,月和

天?

假设应用程序在具有不同Windows的计算机上运行

设置日期/时间格式。



Okay, lets say the date string is: "07-11-05 10:19:26".
How does the simple Parse() method above then know what is year, month and
day?
Assuming that the application runs on computers with different Windows
settings for date/time format.


Kim,
Kim,
string strDate =" 13-10-05 10:19:26" ;;
DateTime dtmDate = DateTime.Parse(strDate);
string strDate = "13-10-05 10:19:26";
DateTime dtmDate = DateTime.Parse(strDate);



好的,我们说日期字符串是:07-11-05 10:19:26。
何w上面的简单Parse()方法然后知道什么是年,月和
日?



Okay, lets say the date string is: "07-11-05 10:19:26".
How does the simple Parse() method above then know what is year, month and
day?




它没有,因此你应该只使用DateTime .Parse(或者如果你使用

VBNet而不是那个CDate的那个),如果它来自一个输入控件

作为文本框(而且它应该是安装文化方式并自动转到




如果你在互联网上使用它,你应该确定你已经展示了

日期时间格式掩码旁边。


您可以在互联网上尝试获取它所在的位置(这不是

标准)和而不是假设文化,但这在我看来是真实的

危险,因为在加拿大实际上是两个完全不同的日期

格式的模式使用。


我希望这会有所帮助,


Cor



It does not, therefore you should only use the DateTime.Parse (or if you use
VBNet the than easier one for that CDate), if it comes from an input control
as the textbox (and than it should be the installed culture way and go
automaticly).

If you use it on internet, than you should be sure that you have showed the
datetime format mask beside the box.

You can on Internet try to get the location from which it comes (that is not
standard) and than assume the culture, however this is in my opinion real
dangerous, because in by instance in Canada are two complete different date
format paterns used.

I hope this helps,

Cor


这篇关于将日期字符串转换为DateTime。请举例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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