比较这个格式的日期时间MM / dd / yyyy h:mm:ss tt [英] comparing datetime in this formatMM/dd/yyyy h:mm:ss tt

查看:807
本文介绍了比较这个格式的日期时间MM / dd / yyyy h:mm:ss tt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要将日期时间与系统时间进行比较,但我的日期时间输出为(MM / dd / yyyy h:mm:ss tt)此格式。所以请帮我比较系统日期时间。



尝试下面的步骤,但没有转换



DateTime dttime;

DateTime.TryParse(4/25/2014 6:44:00 AM,out dttime);

解决方案

系统无法正确识别日期...尝试明确强制日期文化...



 CultureInfo culture = CultureInfo.CreateSpecificCulture( EN-US); 
DateTime dttime;
if(DateTime.TryParse(4/25/2014 6:44:00 AM,culture,DateTimeStyles.None,out dttime))
{
//使用日期;
}


TryParse返回一个bool,告诉它是否有效 - 所以值得检查它。

试试这个:

  bool  working = DateTime.TryParseExact(  2014/4/25 6:44:00
< span class =code-string> G,
new CultureInfo( en-US),
DateTimeStyles.None,
out dttime );


DateTime.ParseExact(yourtime,dd / MM / yyyy hh:mm:ss tt,CultureInfo.InvariantCulture);



根据您的需要变化......例如:MM / dd / yy h:mm:ss tt - 05/12/14 4:50:00 AM

need to compare the datetime with system time, but my datetime output is in (MM/dd/yyyy h:mm:ss tt) this format. so please help me compare with system datetime.

tried below step, bt not getting convert

DateTime dttime;
DateTime.TryParse("4/25/2014 6:44:00 AM", out dttime);

解决方案

The system isn't recognising the date properly ... try explicitly forcing the date culture ...

CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
DateTime dttime;
if (DateTime.TryParse("4/25/2014 6:44:00 AM", culture, DateTimeStyles.None, out dttime))
{
     //use the date;
}


TryParse returns a bool which says if it works - so it's worth checking it.
Try this instead:

bool worked = DateTime.TryParseExact("4/25/2014 6:44:00 AM",
                                     "G", 
                                     new CultureInfo("en-US"),
                                     DateTimeStyles.None,
                                     out dttime);


DateTime.ParseExact (yourtime,"dd/MM/yyyy hh:mm:ss tt" ,CultureInfo.InvariantCulture );

change according your need...eg: MM/dd/yy h:mm:ss tt - 05/12/14 4:50:00 AM


这篇关于比较这个格式的日期时间MM / dd / yyyy h:mm:ss tt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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