Sunday, June 26, 2011

Formating dates and times in VB6



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 betweennoon 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 betweennoon 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


Tuesday, June 21, 2011

Fungsi-fungsi String pada VB

Berhubung saya sendiri baru belajar menggunakan Visual Basic, dan kebetulan saya mendapatkan sedikit masalah ketika akan mengambil nilai dari suatu karakter di VB, saya mau coba share fungsi-fungsi string pada VB yang saya peroleh dari bang irmanf, ya sekalian kalo lupa saya bisa langsung cari dari blog saya sendiri… Hehe… Semoga saja bisa membantu teman-teman yang lain yang sama-sama lupa sintaks seperti saya… hehe…

Fungsi-fungsi VB 6 di bawah ini digunakan untuk mengolah data string.

Left : mengambil n karakter di sebelah kiri suatu string
karakter = Left(“abcdef”,2) ‘karakter = “ab”

Right : mengambil n karakter di sebelah kanan suatu string
karakter = Right(“abcdef”,2) ‘karakter = “ef”

Trim : menghilangkan spasi kosong di awal dan akhir suatu string
karakter = Trim(“ abc def ”) ‘karakter = “abc def”

Ltrim : menghilangkan spasi kosong di awal suatu string
MyStr = Ltrim(AnyString)

Rtrim : menghilangkan spasi kosong di akhir suatu string
MyStr = Rtrim(AnyString)

Ucase : mengubah suatu string menjadi huruf besar semua
MyStr = UCase(AnyString)

Lcase : mengubah suatu string menjadi huruf kecil semua
MyStr = LCase(AnyString)

Mid : mengambil n karakter dari suatu posisi yang ditetapkan
MyStr = Mid(“abcdefghij”, 3, 4) ‘hasil “cdef”

Len : menghitung jumlah karakter yang membentuk suatu string
MyStr = Len(“abcdef”) ‘hasil=6

LSet : menempatkan string di dalam string yang lain, di sebelah kiri
MyStr = “0123456789”
Lset MyStr = “<-Left” ‘hasil “<-Left “

RSet : menempatkan string di dalam string yang lain, di sebelah kanan
MyStr = “0123456789”
Rset MyStr = “>-Right” ‘hasil “ >-Right“

Format : mengatur string sehingga terformat sesuai yang ditentukan
A$ = Format (5455.4, “##,##0.00”) ‘A$ = “5,459.40”
A$ = Format (334.9, “####.##”) ‘A$ = “334.9”
A$ = Format (5, “0.00%”) ‘A$ = “500.00%”
A$ = Format (“HELLO”, “<”) ‘A$ = “hello”
A$ = Format (“This is”, “>”) ‘A$ = “THIS IS”

String: membuat string yang berisi sejumlah karakter yang digandakan
A$ = String (5, “*”) ‘A$ = “*****”

Chr: menghasilkan karakter yang terwakili oleh suatu angka tertentu
A$ = Chr (65) ‘A$ = A
A$ = Chr (97) ‘A$ = a
A$ = Chr (62) ‘A$ = >

Asc: menghasilkan angka ASCII dari suatu karakter tunggal
MyNumber = Asc(‘A’) ‘’hasilnya 65
MyNumber = Asc(‘a’) ‘’hasilnya 97
MyNumber = Asc(‘Apple’) ‘’hasilnya 65

Space: menghasilkan ruang kosong sebanyak n karakter
MyStr = Space(10) ‘buat string 10 spasi
MyStr = “Hello” & Space(10) & “World” ‘menyisipkan 10 spasi diantara kata Hello World

InStr: menentukan apakah string tertentu berada pada string lain
Dim CariString, CariChar, MyPos
CariString = ‘XXpXXpXXPXXP” ‘String yang dianalis
CariChar = “P” ‘String yang dicari “P”
‘mencari mulai dari kolom ke-4, hasilnya 6
MyPos = InStr(4, CariString, CariChar, 1)
‘mencari mulai dari kolom ke-1, hasilnya 9
MyPos = InStr(1, CariString, CariChar, 0)
MyPos = InStr(CariString, CariChar) ‘hasilnya 9
MyPos = InStr(1, SearchString, “W”) ‘hasilnya 0

InStrRev: cari posisi string dalam string yang lain, mulai dari akhir
i = InStrRev(StringCheck, StringMatch[, start[, compare]])

StrComp: membandingkan dua variabel string
StrComp(string1, string2 [, compare] )

Jika
Hasilnya
string1 < string 2
-1
string1= string 2
0
string1> string 2
1
string1atau string 2 = Null
Null

Dim MyStr1, MyStr2, MyComp
MyStr1 = “ABCD” : MyStr2 = “abcd” ‘nilai awal
A = StrComp(MyStr1, MyStr2, 1) ‘A = 0
A = StrComp(MyStr1, MyStr2, 0) ‘A = -1
A = StrComp(MyStr2, MyStr1) ‘A = 1

StrConv: mengubah huruf besar atau kecil suatu karakter string
A$ = StrConv(“Semua Besar”, 1) ‘A$ = “SEMUA BESAR”
A$ = StrConv(“Semua Kecil”, 2) ‘A$ = “semua kecil”
A$ = StrConv(“pertama BESAR”, 3) ‘A$ = “Pertama Besar”

StrReverse: mengubah urutan karakter suatu string
A$ = StrReverse(“12345678”) ‘A$ = “87654321”
A$ = StrReverse(“abcdefg”) ‘A$ = “gfedcba”

Replace: menggantikan string dari kelompok string
Replace(expression, find, replace[, start[, count[, compare]]])

FormatCurrency: string memakai format currency yang ditetapkan
A$ = FormatCurrency(12000, 1) ‘A$ = “$12,000.0”
A$ = FormatCurrency(12000, 2) ‘A$ = “$12,000.00”
Catatan, untuk mengubah mata uang, gunakan Regional Settings Currency dari sistem operasi Windows

FormatDateTime: menghasilkan ekspresi tanggal dan waktu
A$ = FormatDateTime(Now) ‘hasilnya “10/8/02 11:15:46 AM”
A$ = FormatDateTime(Now, vbLongDate) ‘hasilnya “Tuesday, March 02, 2008”
A$ = FormatDateTime(“3/2/99”, vbShortDate) ‘hasilnya “3/2/99”
A$ = FormatDateTime(“3/2/99”, vbLongDate) ‘hasilnya “12:00:00 AM”

FormatNumber: membuat format bilangan sesuai option yang diberikan
FormatNumber(var1, 2)

FormatPerCent: membuat format bilangan dalam prosentase
A$ = FormatPerCent(0.1255, 2) ‘A$ = 12.55%
A$ = FormatPerCent(0.12555) ‘A$ = 12.56%
A$ = FormatPerCent(12.55, 2) ‘A$ = 1,255.00%
A$ = FormatPerCent(12.55) ‘A$ = 1,255.00%

Sebelumnya makasih banyak buat bang irmanf yang memberikan sedikit ilmunya kepada saya… Semoga dapat bermanfaat… :D

~Talk Less Do More ~

http://liemien.wordpress.com/2011/05/09/fungsi-fungsi-string-pada-vb/

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Blogger Templates