以YYYY-MM-DD格式的日期DateTime.TryParse问题 [英] DateTime.TryParse issue with dates of yyyy-dd-MM format

查看:717
本文介绍了以YYYY-MM-DD格式的日期DateTime.TryParse问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在字符串格式2011-29-01 12:00 AM下面的日期。现在我想将其转换成日期时间格式有以下code:

I have the following date in string format "2011-29-01 12:00 am" . Now I am trying to convert that to datetime format with the following code:

DateTime.TryParse(dateTime, out dt); 

但我alwayws越来越DT为{1/1/0001 12:00:00 AM},你能告诉我为什么吗?以及如何我说字符串转换为日期。

But I am alwayws getting dt as {1/1/0001 12:00:00 AM} , Can you please tell me why ? and how can I convert that string to date.

编辑:我刚才看到大家提到的使用format参数。现在我会提到,我不能使用format参数,因为我有一些设置来选择日期格式是什么用户想要自定义,并基于用户能够通过jQuery日期选择器自动获得在文本框的日期在该格式。

I just saw everybody mentioned to use format argument. I will mention now that I can't use the format parameter as I have some setting to select the custom dateformat what user wants, and based on that user is able to get the date in textbox in that format automatically via jQuery datepicker.

推荐答案

这应该工作的基础上你的榜样2011-29-01 12:00 AM

This should work based on your example "2011-29-01 12:00 am"

DateTime dt;
DateTime.TryParseExact(dateTime, 
                       "yyyy-dd-MM hh:mm tt", 
                       CultureInfo.InvariantCulture, 
                       DateTimeStyles.None, 
                       out dt);

这篇关于以YYYY-MM-DD格式的日期DateTime.TryParse问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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