dim H, M, S, AMPM as strings

DTPicker1.Hour = H
DTPicker1.Minute = M
DTPicker1.Second = S
DTPicker1.?????? = AMPM

im having a problem on how i can change the DTpickers AM/PM in codes

im trying to load a time from a database to DTPicker,

ive Split the (field loaded on the database) to H M S
and also AMPM

pls help me on this guys... thanks in advance

Dani AI

Generated

A quick, reliable approach: the DTPicker does not have a separate AM/PM property — the control displays AM/PM according to the Date/Time value you assign. Rather than trying to set Hour/Minute/Second properties (those aren�t writable on the standard control), parse the stored string, convert the parsed hour into 24‑hour form (handle the 12 AM / 12 PM edge cases), then build a Date value and assign it to the picker. This keeps display/formatting (what and were pointing at) separate from the actual value.

Example (VB6-style) — parse the DB string, adjust for AM/PM, and set the picker value:

' parse "hh:mm:ss AM" or "hh:mm:ss PM" and set dtMDR.Value
Dim raw As String
raw = RSSchedule.Fields("MidnightTIME").Value   ' e.g. "12:05:30 PM"

Dim parts() As String
parts = Split(Trim(raw), ":")

Dim h As Integer, m As Integer, s As Integer
h = Val(parts(0))
m = Val(parts(1))

Dim secAmp() As String
secAmp = Split(Trim(parts(2)), " ")
s = Val(secAmp(0))
Dim ampm As String
ampm = UCase(Trim(secAmp(1)))

If ampm = "PM" And h < 12 Then h = h + 12
If ampm = "AM" And h = 12 Then h = 0

dtMDR.Value = DateSerial(Year(Date), Month(Date), Day(Date)) + TimeSerial(h, m, s)

Notes and troubleshooting:

  • In VB6 declare each variable with its type (e.g. Dim h As Integer, ampm As String).
  • Use Trim/Val (or CInt) and validate inputs from the DB.
  • If using VB.NET, build a DateTime with New DateTime(...) instead of DateSerial/TimeSerial.
  • Formatting (12-hour vs 24-hour) is a display setting on the control; AM/PM itself comes from the DateTime value.

Recommended Answers

All 7 Replies

I believe this should get you started...

DTPicker1.Format = dtpCustom
DTPicker1.CustomFormat = "MM/dd/yyyy hh:mm:ss tt"

Good Luck

thx for the reply sir,

but im loading the data i stored in a database to the DTpicker,

how can i set my (strings) to HH:MM:SS TT?

remove the date part of my suggestion...

Good Luck

still cant get it,

heres my code sir

Private Sub SchedMidnightTime()
TOT = RSSchedule.Fields("MidnightTIME")

Me.Show
i = Split(TOT, ":")
H = i(0)
M = i(1)
S = i(2)

d = Split(S, " ")
S = d(0)
ampm = d(1)

'Debug.Print S
'dtMDR is my DTpicker

dtMDR.Hour = H
dtMDR.Minute = M
dtMDR.Second = S
'after ive loaded the Hours,Minutes,Seconds and am/pm  from a 'database i dont know how to load  [QUOTE]ampm[/QUOTE]?
DTPicker1.Format = dtpCustom
DTPicker1.CustomFormat = "hh:mm:ss tt"
DTPicker1.Value = "10:55:23 AM"

Good Luck

commented: thx +1

i hope you try
Text1.Text = Format(DTPicker1, "hh:mm:ss AMPM")

How can i insert a DTPicker in vb4?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.