Formating dates and times in VB6

Level:
Level1

Written By TheVBProgramer.

$ReqTestHarness$

In addition to the named date/time formats described previously, you can also create custom date/time formats using a combination of specific characters recognized by the Format$ function, shown in the table below:

 

Format Character(s)

 

Description

:

Time separator. In some locales, other characters may be used to represent the time separator. The time separator separates hours, minutes, and seconds when time values are formatted. The actual character used as the time separator in formatted output is determined by your system settings.

/

Date separator. In some locales, other characters may be used to represent the date separator. The date separator separates the day, month, and year when date values are formatted. The actual character used as the date separator in formatted output is determined by your system settings.

C

Display the date as ddddd and display the time as t t t t t, in that order. Display only date information if there is no fractional part to the date serial number; display only time information if there is no integer portion.

d

Display the day as a number without a leading zero (1 - 31).

dd

Display the day as a number with a leading zero (01 - 31).

ddd

Display the day as an abbreviation (Sun - Sat).

dddd

Display the day as a full name (Sunday - Saturday).

ddddd

Display the date as a complete date (including day, month, and year), formatted according to your system's short date format setting. The default short date format is m/d/yy.

dddddd

Display a date serial number as a complete date (including day, month, and year) formatted according to the long date setting recognized by your system. The default long date format is mmmm dd, yyyy.

w

Display the day of the week as a number (1 for Sunday through 7 for Saturday).

ww

Display the week of the year as a number (1 - 53).

m

Display the month as a number without a leading zero (1 - 12). If m immediately follows h or hh, the minute rather than the month is displayed.

mm

Display the month as a number with a leading zero (01 - 12). If m immediately follows h or hh, the minute rather than the month is displayed.

mmm

Display the month as an abbreviation (Jan - Dec).

mmmm

Display the month as a full month name (January - December).

q

Display the quarter of the year as a number (1 - 4).

y

Display the day of the year as a number (1 - 366).

yy

Display the year as a 2-digit number (00 - 99).

yyyy

Display the year as a 4-digit number (100 - 9999).

h

Display the hour as a number without leading zeros (0 - 23).

hh

Display the hour as a number with leading zeros (00 - 23).

n

Display the minute as a number without leading zeros (0 - 59).

nn

Display the minute as a number with leading zeros (00 - 59).

s

Display the second as a number without leading zeros (0 - 59).

ss

Display the second as a number with leading zeros (00 - 59).

t t t t t

Display a time as a complete time (including hour, minute, and second), formatted using the time separator defined by the time format recognized by your system. A leading zero is displayed if the leading zero option is selected and the time is before 10:00 A.M. or P.M. The default time format is h:mm:ss.

AM/PM

Use the 12-hour clock and display an uppercase AM with any hour before noon; display an uppercase PM with any hour between noon and 11:59 P.M.

am/pm

Use the 12-hour clock and display a lowercase AM with any hour before noon; display a lowercase PM with any hour between noon and 11:59 P.M.

A/P

Use the 12-hour clock and display an uppercase A with any hour before noon; display an uppercase P with any hour between noon and 11:59 P.M.

a/p

Use the 12-hour clock and display a lowercase A with any hour before noon; display a lowercase P with any hour between noon and 11:59 P.M.

AMPM

Use the 12-hour clock and display the AM string literal as defined by your system with any hour before noon; display the PM string literal as defined by your system with any hour between noon and 11:59 P.M. AMPM can be either uppercase or lowercase, but the case of the string displayed matches the string as defined by your system settings. The default format is AM/PM.

 

 

To demonstrate custom numeric formats using combinations of the characters listed above, set up another "Try It" project, and place the following code in the cmdTryIt_Click event:

 

Private Sub cmdTryIt_Click()

 

Print "Using 'm/d/yy':"; Tab(30); Format$(Now, "m/d/yy")

Print "Using 'mm/dd/yyyy':"; Tab(30); Format$(Now, "mm/dd/yyyy")

Print "Using 'yyyy-mm-dd':"; Tab(30); Format$(Now, "yyyy-mm-dd")

Print "Using 'dddd, mmmm dd, yyyy':"; Tab(30); Format$(Now, "dddd, mmmm dd, yyyy")

Print "Using 'd-mmm':"; Tab(30); Format$(Now, "d-mmm")

Print "Using 'mmmm-yy':"; Tab(30); Format$(Now, "mmmm-yy")

Print "Using 'hh:mm AM/PM':"; Tab(30); Format$(Now, "hh:mm AM/PM")

Print "Using 'h:mm:ss a/p':"; Tab(30); Format$(Now, "h:mm:ss a/p")

Print "Using 'd-mmmm h:mm':"; Tab(30); Format$(Now, "d-mmmm h:mm")

Print "Using 'd-mmmm-yy':"; Tab(30); Format$(Now, "d-mmmm-yy")

Print "Using 'd mmmm':"; Tab(30); Format$(Now, "d mmmm")

Print "Using 'mmmm yy':"; Tab(30); Format$(Now, "mmmm yy")

Print "Using 'hh:mm AM/PM':"; Tab(30); Format$(Now, "hh:mm AM/PM")

Print "Using 'h:mm:ss a/p':"; Tab(30); Format$(Now, "h:mm:ss a/p")

Print "Using 'h:mm':"; Tab(30); Format$(Now, "h:mm")

Print "Using 'h:mm:ss':"; Tab(30); Format$(Now, "h:mm:ss")

Print "Using 'm/d/yy h:mm':"; Tab(30); Format$(Now, "m/d/yy h:mm")

End Sub

 

Run the project and click the "Try It" button. The current date and/or time, formatted in the various ways, will be displayed on your form:

 

 

 

Download the VB project code for the example above here.

If you enjoyed this post, subscribe for updates (it's free)

Date Time Control

A Date time picker control shows a date in the following format:
dd MMM yyyy
Sample Date:08 Feb 2014
A user can type month as a number in this control but is not able to type it as a Three character Abbrevation.If the user types '02' then it gets converted into 'Feb' but 'Feb' cannot be directly entered.The control should accept the three character abbr too.

how to get the total number of years, month and day...

hi to all,

hope you can help me regarding on my problem, im trying to make a simple human resources system here in our office and my problem is how can get the exact total numbers of years in service of our employee including the month and date.... anyway im using dtpicker for my date of employment and retirement date tab....

thanks in advance...

date format

how can i get date format as below

yyyy-mm-ddThh:mm:ss+05:30

for example 1900-01-01T01:01:01+05:30

subtraction

How to subtract previous date from current date in vb6.0

NEED HELP8!!!!!!!!!!!!!!!!!!!!!!!!!!!!!NOW (With VB Date Format)

TIME IS WHEN? WHEN TO THINK OF DATE INT?
IS POSSIBLE? YES NO

TIME IN COUNTRIE IS FIEV HOUR STRAIHGT
NO CHANGE NO THICK

IT IS POSSIBLE NOT

MAYBE HE IS SECODN?? OR WHAT

EXPLIAN NAW8 NEED ANWSER NOW!8

I THINK YOU GOT NO? OR YES? SEE

DUCKS IN MOUTH OR GUESS YET

SO TIME NOW AND SEE DATE OF HOUR PLUS MACOR

SI

is it possible

is it possible to create a vb program that identifies my machine name and myIP, my host IP also the names of other machines that are connected to the same network? If it is I would like to see the code of that program

NOT

IS POSSIBLE

Format function Equivalent form VB6 to VB.net

Hi,
What is the Equivalent Functionality form VB6 to VB.net for this Format Function

Format$("123", "@@@@@")

can anybody Help me.

Aru

Visual Basic

Crear un programa que permita editar el texto en una etiqueta (label). El programa debe usar “radio buttons” para el tipo de letra y para el color de la letra. Las opciones para el tipo de letra son negrilla, itálica y normal. Las opciones de colores son azul, rojo y verde.

How to calculate between DateTime and Minute or hour?

Hi,

Need your help with vb6 code.
i have textbox text1.text shown date now. example. 09/21/2011 18:15:00
i have textbox text2.text shown minute number. for example "1"
when i click the command button, i need textbox text3.text as result of text1 & text 2 calculation . and the result should be
09/21/2011 18:16:00.

Please help me how to make vb code for condition above?
thanks in advance

nia ma ci bai

nia ma ci bai

about date and time

am newly using vb6 can plz help me some any one
how to put in a form that time and date
....................................................
.

VB

I am having lastdate,nextdate, interval(months)

if i given lastdate and the interval it shoud show the nextdate

ex: lastdate is 05/07/2011 and the interval is 2(Months)

the display in nextdate should be :05/09/2011

please help me

Convert months to Quarters

Please help me to write a program to convert months to Quarters.
Example 1,2,3,=Q1
4,5,6 = Q2
7,8,9 =Q3
10,11,12 =Q4

simple

q=int(m/4)

where q=quarters
m=months
cheers from oz

to display time

hi guys ....
i thnk some of the guys are so worried how to display time like minutes hours and seconds

il show u a simple method to display time

place a textbotx in c# r asp r vb

then double click on the form and write code like this

textbox1.text=time

displays time like
3.48.23 pm

but here u the seconds are constant so to see the seconds also place a timer contorl on the form change its interval to 1000 and double click on the timer the timer form will be opend write code in timer contro

textbox1.text=time

displays the time hope u guys like thisss
thank you .......

santosh kumar
hyderabad
6/05/2011
3.51pm

Any One To Help out with STOPWATCH?

I'm just a beginner and I want to put together a stopwatch. Can you help please? Reach me on PastorItohanHenry@yahoo.com
THANKS

I have a question: minute is "mm" or "nn"?

Hi, i have a question: the example uses "mm" with minutes, the table says that minutes are with "nn", what is the correct? (i think "nn", because "mm" is used with months)...

Thanks

hi can you help me in my

hi can you help me in my problem?

I want to run automatically my program f I set the time from 8:00am then it well also automatically end when I set in 5:00pm how make it..please help me......thank you......god bless

Try something like

Try something like this:
(call this every second from a timer)

Dim TimeDiff As Long
TimeDiff = DateDiff("s", "17:00:00", Time())
If (TimeDiff = 0) Then End
' if you want to reboot your computer, do this instead...
If (TimeDiff = 0) Then Shell "shutdown -r -t 20" ' Restart computer in 20 seconds

hi there..Guys nid

hi there..Guys nid help...don't understand all d codes you post..please post simple codes that display time and date..?.Y_Y..

Here yah go

Private Sub Timer1_Timer()
Label1.Caption = Format$(Now, "mmmm dd, yyyy (dddd)") & " " & Time
End Sub

Private Sub Form_Load()
Timer1.Interval = 1

the output will be:

January 29, 2011 8:26:11

Help..

help
i have 3 textbox, i will put date on 2 textbox
example the date on date1.text = now(w/c is 1/17/2011), date2.text = 1/20/2011
the other text box is status.text=suspended
is it possible that everyday, the date1.text will subtract on date2.text automatically?
so that when 1/21/2011 comes, status.text will be cleared..

Thanks

thank you very much

@Rimpu

Form_Load() 'use example:
  msgbox DateAsText(Format$(Now, "mm/dd/yyyy"))
End Sub
 
Function DateAsText(ByVal DateIn As String)
 
    Dim s As Variant
    Dim DaysA As Variant
        DaysA = Array("", "first", "two", "three", "four", "five", "six", "seven", "eight", "nine", "tenenth", "eleven", "twelve", "thirtneen", "fourteen", _
                      "fifeteen", "sixteen", "seventeen", "eighteen", "nineteen", "twentieth", "twenty one", "twenty two", "twenty three", _
                      "twenty four", "twenty five", "twenty six", "twenty seven", "twenty eight", "twenty nine", "thirtieth", "thirty one")
    Dim MonthsA As Variant
        MonthsA = Array("", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
 
    s = Split(DateIn, "/")
    If UBound(s) < 2 Then DateAsText = "Invalid date": Exit Function
 
    DateAsText = MonthsA(s(0)) & " - " & DaysA(s(1)) & " - "
 
    If s(2) = "2010" Then DateAsText = DateAsText & "two thousand and ten"
    If s(2) = "2011" Then DateAsText = DateAsText & "two thousand and eleven"
    If s(2) = "2012" Then DateAsText = DateAsText & "two thousand and twelve"
    If s(2) = "2013" Then DateAsText = DateAsText & "two thousand and thirteen"
 
End Function

Need help remembering VB date format as an integer

Back in 2004 I was using programming with VB6 and I used to use a combination that gave me an integer that included the day, the month and the time. I can not remember how I did it but it was someting like Cdat(date$+time$) and it produced a number like 101.190 where the whole part number was the date and the decimal was the hour. Could you help me with this.
Ray

Help

Please help me
It is urgent for my project

I want to know the mid date between two dates.
For Example if Text1.text = 01/01/2008 and Text2.text = 06/06/2010 then I need a vb code to calculate the mid date between these two dates.

Thanks!

DateDiff Function

Without thinking about it too much I would use the DateDiff() and DateAdd() functions to achieve the desired outcome here.
First find the difference between Date1 and Date2 in days with the DateDiff() function, then add half this number of days to Date1.
e.g.

Private Sub Command1_Click()
 
Dim intDaysDiff     As Integer
Dim dteMidDate      As Date
 
intDaysDiff = DateDiff("d", CDate(Text1.Text), CDate(Text2.Text))
 
dteMidDate = DateAdd("d", intDaysDiff / 2, CDate(Text1.Text))
 
Text3.Text = Format$(dteMidDate, "dd/mm/yyyy")
 
End Sub

If the first Date1 is larger than Date2 the DateDiff() function will return a negative number so the code sorts itself out and still finds MidDate without a problem. I have also included a line at the bottom of the code example to format the resulting date to a string and display it in a third text box. The above example requires a command button to trigger the calculation.

vb6 convert time to decimal

hello can i have a code that
converts time to decimal
like:
11:30 - 11.5
12:00 - 12.0
1:30 - 1.5

pls...
thank you...

few other date functions (for VB date format)

Few more required functions for date in VB6 are:

1. DateDiff
2. DatePart
3. DateSerial
4. DateValue

These inbuilt functions are also very useful in calculating the Dates.

If you are using MS SQL Server, you may use Format(DateField,"dd-MMM-yy"), this will always give accuate date, according to the date format setted in your control panel. You can customized your vb date format from Regional & Language Settings.

Preferable setting is dd/MM/yyyy.

Regards

Chandresh Kumar Chhatlani ( A programmer in Udaipur City of Rajasthan State in INDIA)
+91 99285 44749
chandesh_kumar@india.com
http://chandreshkumar.wetpaint.com

Found the answer for the Month problem

After I posted my comment, I decided to try capital M's to see how that might work. That solved the problem. Please update your format chart to reflect the difference between small and upper case M's in the Format command. Capital M's are for the month and small m's are for the minutes.

mm format only gives me minutes, not month

I have this code line to create an upload file name based on the time
txtFileNameToSaveAs = txtLName.Text + "_" + txtFName.Text + "_" + txtJobClass.Text + "_" + Format$(Now, "yyyy-mmm-dd") + ".doc"
regardless of what I do with the mmm or mm, I get the timestamp minute, not the month. The filename on the last try became:
Hendela_Arthur H _Start-up_2009-24-09.doc
instead of "Hendela_Arthur H_Start-up_2009-Jul-09.doc.
How does one correct this?
Thanks.

Instead of your string

Instead of your string being:
txtFileNameToSaveAs = txtLName.Text + "_" + txtFName.Text + "_" + txtJobClass.Text + "_" + Format$(Now, "yyyy-mmm-dd") + ".doc"

It should be:
txtFileNameToSaveAs = txtLName.Text + "_" + txtFName.Text + "_" + txtJobClass.Text + "_" + Format$(Now, "yyyy-MMM-dd") + ".doc"

Try that...

2 DTpicker set to first and last day of the current month

Private Sub Form_Load()
DTPicker1.Value = DateAdd("d", -(Day(Date) - 1), Date)
DTPicker2.Value = DateAdd("d", (getNumberOfDays - 1), DTPicker1.Value)
End Sub

'Get the number of days each month is having
Public Function getNumberOfDays() As Integer
Select Case DateTime.Month(Date)
Case 1, 3, 5, 7, 8, 10, 12
getNumberOfDays = 31
Case 4, 6, 9, 11
getNumberOfDays = 30
Case 2
'logic for checking leap years
If (Year(Date) Mod 4) = 0 Then
If (Year(Date) Mod 100) = 0 Then
If (Year(Date) Mod 400) = 0 Then
getNumberOfDays = 29
Else
getNumberOfDays = 28
End If
Else
getNumberOfDays = 29
End If
Else
getNumberOfDays = 28
End If
End Select
End Function

THANK YOU....

It Helped me a lot...

Great post.

Great post.

please help

i would like to seek help regarding this situation.,

i want to make a validation, in which if the time reached the "8am" the command button time-in will be activate and vice versa when the time reached the 12pm the command button time-out will be activate and the command button time-in will be deactivate.
thanks.,

VB6 date change into text

Hello
i want to change date in text
example
- 01/01/2009
- one january two thousand nine

rs1985@rediffmail.com

I can solve your problem. Do

I can solve your problem. Do you get any solution about date convert in word? you can communication me.