如何转换字符串? [英] How to Convert string?

查看:65
本文介绍了如何转换字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串variabl,其中包含当前时间(例如3:20 PM),我想创建一个if条件,例如if(string>"9:15 AM"),然后执行其他操作,如果我使用此代码表示操作符>无法应用于字符串

I have a string variabl which contains current time (e.g 3:20 PM),i want to make an if condition like if(string > "9:15 AM") then do this else do something else,If i use this code it says that operator > cannot applied to strings

How can i convert this string to Time??

推荐答案

^ ]可能是最好的解决方案

问候
Espen Harlinn
DateTime.TryParse[^] is probably the best solution

Regards
Espen Harlinn




试试这个快速又肮脏的解决方案:
Hi,

try this quick and dirty solution:
string str1 = "3:20 PM";
DateTime time1;
if(DateTime.TryParse(str1, out time1))
{
  //It is a Valid DateTime
  DateTime time2 = new DateTime();
  time2 = time2.AddHours(9);
  time2 = time2.AddMinutes(15);
  if (time1.TimeOfDay > time2.TimeOfDay)
    MessageBox.Show("Time is later");
}


以下是您的链接

^ ]
Here is the link for you

Here[^]


这篇关于如何转换字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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