help with datetime

  • Thread starter Thread starter Mohan
  • Start date Start date
M

Mohan

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
 
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
 
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);
 
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
 
Mohan said:
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.

As others have already said, change the format you are storing the date
values in. You may want to take a look at the 'SqlDateTime' type too.
 
Back
Top