如何在VB6中格式化标签以显示12小时时间格式而不显示AM/PM? [英] How Do I Format a Label to Display 12 Hour Time Format Without the AM/PM in VB6?
问题描述
我正在使用Visual Basic 6创建一个时钟程序,该程序使用计时器在主窗体的标签中显示时间,并且我有一个选项窗体,用户可以在该窗体上自定义几种设置,包括在12之间更改时间格式.小时和24小时格式.如果选择了12小时格式,我也想允许一个人切换AM/PM的显示.在文档中没有早上/下午标记,没有12小时的选项(我可以找到),因此,我的问题是:如何在没有AM和PM的情况下以12小时格式显示时间?除此之外,一切都在工作.
请从下面的选项表和下面的公共变量声明中引用代码.
I am creating a clock program using Visual Basic 6 that is using a timer to show the time in a label on my main form and I have an options form on which users can customize several settings, including changing the time format between 12 Hour and 24 Hour format. If 12 hour format is choosen, I would also like to allow a person to toggle the display of the AM/PM. There is no option (from what I can find) for 12 hour time without the morning/afternoon marker in the documentation My question, therefore, is this: how do I display the time in 12 hour format without the AM and PM? All is working except this.
Please reference the code from my options form below and the Public variable declarations below.
模块中的变量声明
Public strTimeFormat As String, strDateFormat As String
Public Const strSecond = "ss"
Public Const strAMPM = "AMPM"
Public Const strColon = ":"
Public strCustom As String, strHourCurrentSelection As String, strMinuteCurrentSelection As String, strTimeCurrentSelection As String
选项表格(frmOptions
)
Option Explicit
Private Sub chkAMPM_Click()
If chkAMPM.Value = True Then
strTimeCurrentSelection = strHourCurrentSelection + strColon + strMinuteCurrentSelection + strAMPM
End If
End Sub
Private Sub chkSeconds_Click()
If chkSeconds.Value = True Then
strTimeCurrentSelection = strHourCurrentSelection + strColon + strMinuteCurrentSelection + strSecond
Else
strTimeCurrentSelection = strHourCurrentSelection + strColon + strMinuteCurrentSelection
End If
End Sub
Private Sub cmdApply_Click()
strTimeFormat = strTimeCurrentSelection
End Sub
Private Sub cmdCancel_Click()
Unload Me
End Sub
Private Sub cmdOK_Click()
MsgBox "Place code here to set options and close dialog!"
Unload Me
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Dim i As Integer
'handle ctrl+tab to move to the next tab
If Shift = vbCtrlMask And KeyCode = vbKeyTab Then
i = tbsOptions.SelectedItem.Index
If i = tbsOptions.Tabs.Count Then
'last tab so we need to wrap to tab 1
Set tbsOptions.SelectedItem = tbsOptions.Tabs(1)
Else
'increment the tab
Set tbsOptions.SelectedItem = tbsOptions.Tabs(i + 1)
End If
End If
End Sub
Private Sub Form_Load()
'center the form
Me.Move (Screen.Width - Me.Width) / 2, (Screen.Height - Me.Height) /
End Sub
Private Sub opt12Hour_Click()
opt24NoZero.Enabled = False
opt24Zero.Enabled = False
chkColon.Enabled = False
txtCustomTimeLayout.Enabled = False
txtCustomTimeLayout.BackColor = &H8000000F
opt12NoZero.Enabled = True
opt12Zero.Enabled = True
chkAMPM.Enabled = True
End Sub
Private Sub opt12NoZero_Click()
strHourCurrentSelection = ""
strMinuteCurrentSelection = ""
strHourCurrentSelection = "h"
strMinuteCurrentSelection = "mm"
strTimeCurrentSelection = strHourCurrentSelection + strColon + strMinuteCurrentSelection
End Sub
Private Sub opt12Zero_Click()
strHourCurrentSelection = ""
strMinuteCurrentSelection = ""
strHourCurrentSelection = "hh"
strMinuteCurrentSelection = "mm"
strTimeCurrentSelection = strHourCurrentSelection + strColon + strMinuteCurrentSelection
End Sub
Private Sub opt24Hour_Click()
opt12NoZero.Enabled = False
opt12Zero.Enabled = False
chkAMPM.Enabled = False
txtCustomTimeLayout.Enabled = False
txtCustomTimeLayout.BackColor = &H8000000F
opt24NoZero.Enabled = True
opt24Zero.Enabled = True
chkColon.Enabled = True
End Sub
Private Sub opt24NoZero_Click()
strHourCurrentSelection = ""
strMinuteCurrentSelection = ""
strHourCurrentSelection = "H"
strMinuteCurrentSelection = "mm"
strTimeCurrentSelection = strHourCurrentSelection + strColon + strMinuteCurrentSelection
End Sub
Private Sub opt24Zero_Click()
strHourCurrentSelection = ""
strMinuteCurrentSelection = ""
strHourCurrentSelection = "HH"
strMinuteCurrentSelection = "mm"
strTimeCurrentSelection = strHourCurrentSelection + strColon + strMinuteCurrentSelection
End Sub
Private Sub optCustomTime_Click()
opt12NoZero.Enabled = False
opt12Zero.Enabled = False
chkAMPM.Enabled = False
opt24NoZero.Enabled = False
opt24Zero.Enabled = False
chkColon.Enabled = False
txtCustomTimeLayout.Enabled = True
txtCustomTimeLayout.BackColor = &H80000009
End Sub
Private Sub tbsOptions_Click()
Dim i As Integer
'show and enable the selected tab's controls
'and hide and disable all others
For i = 0 To tbsOptions.Tabs.Count - 1
If i = tbsOptions.SelectedItem.Index - 1 Then
picOptions(i).Left = 210
picOptions(i).Enabled = True
Else
picOptions(i).Left = -20000
picOptions(i).Enabled = False
End If
Next
End Sub
Private Sub txtCustomTimeLayout_Change()
strTimeCurrentSelection = txtCustomTimeLayout.Text
End Sub
公共订阅模块
Public Sub Clock()
frmClock.lblTime.Caption = Format(Now, strTimeFormat)
frmClock.lblDate.Caption = Format(Now, strDateFormat)
End Sub
Timer_Click()事件
Timer_Click() Event
Private Sub tmrClock_Timer()
Clock
End Sub
推荐答案
简单方法:
Left$(Format$(Now(), "Hh:NnA/P"), 5)
这篇关于如何在VB6中格式化标签以显示12小时时间格式而不显示AM/PM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!