|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
help with datetimeIn the database, we are storing time in 3 columns: hours, Minutes and
AM,PM. After retrieving it from the database, I want to load these values into a DateTime variable. I haven't had much success. I would appreciate if anyone can help me in this matter thank you MB 1. Consider changing your database to store them using a real datetime
datatype instead 2. Consider changing your database to store them using a real datetime datatype instead 3. Consider changing your database to store them using a real datetime datatype instead 4. Ok, maybe you can't change your database :-) Try this: Dim hours As String = "07" Dim minutes As String = "23" Dim ampm As String = "am" Dim s As String = hours & ":" & minutes & " " & ampm Dim dt As DateTime = DateTime.TryParse(s, "hh\:mm tt", Nothing) Note that the format string in this sample ("hh\:mm tt") assumes leading zeroes for single digit hours and minutes. If you don't have that you can use "h\:m tt", "h\:mm tt" or "hh\:m tt" instead (depending on what you have). See DateTimeFormatInfo for more info on the format string. /claes Show quoteHide quote "Mohan" <kasvi***@gmail.com> wrote in message news:1151501933.445860.290860@x69g2000cwx.googlegroups.com... > In the database, we are storing time in 3 columns: hours, Minutes and > AM,PM. > After retrieving it from the database, I want to load these values into > a DateTime variable. I haven't had much success. > > I would appreciate if anyone can help me in this matter > > thank you > MB > First of all storing hours, minutes, and AM/PM is not sufficient to
correctly reproduce a DateTime object. public DateTime (int year,int month,int day,int hour,int minute,int second,int millisecond) Since in the database you are also storing AM/PM, so I assume you are storing hours in 12 hour format. So before calling DateTime constructor, set the hour field to 24 hours format. If (PM) { hour = (hr_from_DB < 12 ? (hr_from_DB + 12) : 0) ); } <- Note that the day field will still be wrong. DateTime dt = new DateTime(System.DateTime.Today.Year, System.DateTime.Today.Month, System.DateTime.Today.Day, hour, 50, 0, 0); Show quoteHide quote "Mohan" <kasvi***@gmail.com> wrote in message news:1151501933.445860.290860@x69g2000cwx.googlegroups.com... > In the database, we are storing time in 3 columns: hours, Minutes and > AM,PM. > After retrieving it from the database, I want to load these values into > a DateTime variable. I haven't had much success. > > I would appreciate if anyone can help me in this matter > > thank you > MB > Mohan,
1. Consider changing your database to store them using a real datetime datatype instead 2. Consider changing your database to store them using a real datetime datatype instead 3. Consider changing your database to store them using a real datetime datatype instead 4. Ok, maybe you can't change your database :-) Than you can try this: \\\ Dim dtm As New DateTime dtm = dtm.AddHours(CDbl("11")) dtm = dtm.AddMinutes(CDbl("50")) If "PM" = "PM" Then dtm = dtm.AddHours(12) /// Althouhg I would use one of the first 3 advices. Cor Show quoteHide quote "Mohan" <kasvi***@gmail.com> schreef in bericht news:1151501933.445860.290860@x69g2000cwx.googlegroups.com... > In the database, we are storing time in 3 columns: hours, Minutes and > AM,PM. > After retrieving it from the database, I want to load these values into > a DateTime variable. I haven't had much success. > > I would appreciate if anyone can help me in this matter > > thank you > MB > "Mohan" <kasvi***@gmail.com> schrieb: As others have already said, change the format you are storing the date > In the database, we are storing time in 3 columns: hours, Minutes and > AM,PM. > After retrieving it from the database, I want to load these values into > a DateTime variable. I haven't had much success. values in. You may want to take a look at the 'SqlDateTime' type too. -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/>
Deleting All Selected Checked files
Control image property locking image file even after image cleared Losing accented characters getting account names from system screen resolution independent Hide web service in COM interop assembly DataGrid X DataSource Syntax Error Datasets What do I need to deploy exe to 98/XP machines supress dialog box of a dll in runtime |
|||||||||||||||||||||||