Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Time in C#?
Message
De
21/06/2013 17:34:50
Mike Cole
Yellow Lab Technologies
Stanley, Iowa, États-Unis
 
 
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Titre:
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01576867
Message ID:
01576891
Vues:
33
>>>>I have a character column in the database in this format:
>>>>
>>>>10:00 AM
>>>>
>>>>How can I get hour and minute from it?
>>>>
>>>>Thanks in advance.
>>>>
>>>>I found this RegEx pattern
>>>>
>>>>(1[012]|[1-9]):[0-5][0-9](\\s)?(?i)(am|pm)
>>>>
>>>>but how should I use it to get my groups?
>>>
>>>This is what I came out so far - does it look correct?
>>>
>>>
>>>String cRegEx = "(1[012]|[1-9]):[0-5][0-9](\\s)?(?i)(am|pm)";
>>>MatchCollection matches = Regex.Matches("10:00 AM", cRegEx, RegexOptions.IgnoreCase);
>>>foreach (Match m in matches)
>>>         {
>>>		   Console.WriteLine(m.Groups[0]);
>>>            Console.WriteLine(m.Groups[1]);
>>>			Console.WriteLine(m.Groups[2]);
>>>         }
>>
>>You could use DateTime.Parse to get a DateTime object instead. Easier to read and parse the values from.
>
>Can we pass the time only to datetime method? BTW, my method is not working correctly as I found. Can you please help me to correct it?
var myTime = "10:01 AM";
var myDate = DateTime.Parse(myTime);
var myHours = myDate.Hour;
var myMinute = myDate.Minute;
Very fitting: http://xkcd.com/386/
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform