Hello Everyone,
I have one problem related to date function in vb
i want to print the date in the following format
15th, October-2007
i have already try formatter of the date funtion
Can anyone help me out?? please be fast.
Mayur
9 1888
written in vb. vb.net should be simular... -
Dim a As String
-
a = Mid(System.DateTime.Now.ToString, 1, 10) 'cuts out date value (dd/mm/yyyy)
-
-
Dim b As String
-
b = Mid(a, 4, 2) 'stores month value (mm)
-
-
Dim c As String
-
-
If CInt(b) = "01" Then
-
c = Mid(a, 1, 2) + "th," + " January " + Mid(a, 7, 4)
-
ElseIf CInt(b) = "02" Then
-
c = Mid(a, 1, 2) + "th," + " February " + Mid(a, 7, 4)
-
ElseIf CInt(b) = "03" Then
-
c = Mid(a, 1, 2) + "th," + " March " + Mid(a, 7, 4)
-
ElseIf CInt(b) = "04" Then
-
c = Mid(a, 1, 2) + "th," + " April " + Mid(a, 7, 4)
-
ElseIf CInt(b) = "05" Then
-
c = Mid(a, 1, 2) + "th," + " May " + Mid(a, 7, 4)
-
ElseIf CInt(b) = "06" Then
-
c = Mid(a, 1, 2) + "th," + " June " + Mid(a, 7, 4)
-
ElseIf CInt(b) = "07" Then
-
c = Mid(a, 1, 2) + "th," + " July " + Mid(a, 7, 4)
-
ElseIf CInt(b) = "08" Then
-
c = Mid(a, 1, 2) + "th," + " August " + Mid(a, 7, 4)
-
ElseIf CInt(b) = "09" Then
-
c = Mid(a, 1, 2) + "th," + " September " + Mid(a, 7, 4)
-
ElseIf CInt(b) = "10" Then
-
c = Mid(a, 1, 2) + "th," + " October " + Mid(a, 7, 4)
-
ElseIf CInt(b) = "11" Then
-
c = Mid(a, 1, 2) + "th," + " Novermber " + Mid(a, 7, 4)
-
ElseIf CInt(b) = "12" Then
-
c = Mid(a, 1, 2) + "th," + " December " + Mid(a, 7, 4)
-
End If
-
value stored in c
Written in vb but vb.net should be very similar... - Dim a As String
-
a = Mid(System.DateTime.Now.ToString, 1, 10) 'cuts out date value (dd/mm/yyyy)
-
Dim b As String
-
b = Mid(a, 4, 2) 'stores month value (mm)
-
Dim c As String
-
If CInt(b) = "01" Then
-
c = Mid(a, 1, 2) + " January " + Mid(a, 7, 4)
-
ElseIf CInt(b) = "02" Then
-
c = Mid(a, 1, 2) + " February " + Mid(a, 7, 4)
-
ElseIf CInt(b) = "03" Then
-
c = Mid(a, 1, 2) + " March " + Mid(a, 7, 4)
-
ElseIf CInt(b) = "04" Then
-
c = Mid(a, 1, 2) + " April " + Mid(a, 7, 4)
-
ElseIf CInt(b) = "05" Then
-
c = Mid(a, 1, 2) + " May " + Mid(a, 7, 4)
-
ElseIf CInt(b) = "06" Then
-
c = Mid(a, 1, 2) + " June " + Mid(a, 7, 4)
-
ElseIf CInt(b) = "07" Then
-
c = Mid(a, 1, 2) + " July " + Mid(a, 7, 4)
-
ElseIf CInt(b) = "08" Then
-
c = Mid(a, 1, 2) + " August " + Mid(a, 7, 4)
-
ElseIf CInt(b) = "09" Then
-
c = Mid(a, 1, 2) + " September " + Mid(a, 7, 4)
-
ElseIf CInt(b) = "10" Then
-
c = Mid(a, 1, 2) + " October " + Mid(a, 7, 4)
-
ElseIf CInt(b) = "11" Then
-
c = Mid(a, 1, 2) + " Novermber " + Mid(a, 7, 4)
-
ElseIf CInt(b) = "12" Then
-
c = Mid(a, 1, 2) + " December " + Mid(a, 7, 4)
-
End If
value stored in c
select the following from oracle for the exact format - select to_char(sysdate,'ddth, month yyyy') from dual
i played about with this for a while, teaching myself how to use functions in vb.
i come up with this and it works ok...
--------------------------------------------------------------------------------------------- -
Dim b As String
-
b = System.DateTime.Now.Day
-
Dim c As String
-
c = System.DateTime.Now.Month
-
Dim d As String
-
d = System.DateTime.Now.Year
-
MsgBox(b + " " + getMonth(c) + " " + d)
-
--------------------------------------------------------------------------------------------
AND THEN THE FUNCTION
-------------------------------------------------------------------------------------------- - Function getMonth(ByVal Val)
-
Dim c As String
-
If CInt(Val) = "01" Then
-
c = "January"
-
ElseIf CInt(Val) = "02" Then
-
c = "February"
-
ElseIf CInt(Val) = "03" Then
-
c = "March"
-
ElseIf CInt(Val) = "04" Then
-
c = "April"
-
ElseIf CInt(Val) = "05" Then
-
c = "May"
-
ElseIf CInt(Val) = "06" Then
-
c = "June"
-
ElseIf CInt(Val) = "07" Then
-
c = "July"
-
ElseIf CInt(Val) = "08" Then
-
c = "August"
-
ElseIf CInt(Val) = "09" Then
-
c = "September"
-
ElseIf CInt(Val) = "10" Then
-
c = "October"
-
ElseIf CInt(Val) = "11" Then
-
c = "Novermber"
-
ElseIf CInt(Val) = "12" Then
-
c = "December"
-
End If
-
-
Return c
-
End Function
-
--------------------------------------------------------------------------------------------
Hello Everyone,
I have one problem related to date function in vb
i want to print the date in the following format
15th, October-2007
i have already try formatter of the date funtion
Can anyone help me out?? please be fast.
Mayur
You could try modifying this... -
Dim Str As String, Str1 As String
-
-
Select Case Right$(Format(Now(), "d"), 1)
-
Case Is = "1"
-
Str1 = "st"
-
Case Is = "2"
-
Str1 = "cnd"
-
Case Is = "3"
-
Str1 = "rd"
-
Case Else
-
Str1 = "th"
-
End Select
-
Str = Format(Now(), "d") & Str1 & ", " & Format(Now(), "mmmm-yyyy")
-
-
It is from VB6
I modified it like this... -
Dim b As String
-
b = "2" 'System.DateTime.Now.Day
-
Dim c As String
-
c = System.DateTime.Now.Month
-
Dim d As String
-
d = System.DateTime.Now.Year
-
MsgBox(b + ending(b) + " " + getMonth(c) + " " + d)
-
MsgBox(b + ending(b) + " " + getMonth(c) + " " + d)
and add this function -
Function ending(ByVal Val)
-
Dim a As String
-
If CInt(Val) = "01" Then
-
a = "st"
-
ElseIf CInt(Val) = "02" Then
-
a = "nd"
-
ElseIf CInt(Val) = "03" Then
-
a = "rd"
-
Else
-
a = "th"
-
End If
-
Return a
-
End Function
-
If these numbers relate to the day, then don't forget you have 11th and 21st. Also 12th and 22nd. Also 13th and 23rd. English is such fun, isn't it? :)
I'd recommend the use of a Select Case statement, as it allows very simple handling of multiple cases like this. for example Case 1, 21 to return "st".
Also, in general it would be better practice to execute your function once at the start, placing the result in a variable, then work with that variable while doing the various ElseIf's, rather than performing multiple function calls. Also, it's probably a really bad idea to call a variable Val, given that Val() is also the name of a commonly-used function.
My recommendation for a rewrite of this function would be something like... - Public Function Ending(ByVal DayNum) As String
-
' Dim a As String
-
Select Case CInt(DayNum )
-
Case 1, 21, 31
-
Ending = "st"
-
Case 2, 22
-
Ending = "nd"
-
Case 3, 23
-
Ending = "rd"
-
Case Else
-
Ending = "th"
-
End Select
-
End Function
On the other hand, it would probably make more sense to use an array rather than a function. Why bother doing the work again, every time you access a date?
Hi,
Thank you "jamesd0142" and "Killer42".
I have solved my problem with your help.
As you said killer42 I have made the function so its works fine for me.
See jamesd0142 the whole code will look like this for the VB.
Thanks again. - Private Sub Form_Load()
-
Dim b As String
-
b = Format(Date, "dd") & Ending(Format(Date, "dd"))
-
Dim c As String
-
c = Format(Date, "mm")
-
Dim d As String
-
d = Format(Date, "yyyy")
-
MsgBox b & Format(Date, " - mmmm,yyyy")
-
End Sub
-
-
Public Function Ending(ByVal DayNum) As String
-
Select Case CInt(DayNum)
-
Case 1, 21, 31
-
Ending = "st"
-
Case 2, 22
-
Ending = "nd"
-
Case 3, 23
-
Ending = "rd"
-
Case Else
-
Ending = "th"
-
End Select
-
End Function
Great!
This is the problem with me... my code neva looks this neat :P
I tend to use 53 lines where 12 will do :)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Ed |
last post by:
Hi there,
My problem is the following: When I assign a custom formatted Date & Time to
a Date variable it loses it’s formatting. Ex. 2005-06-07 15:46
now when I assign this to a variable of...
|
by: Paul |
last post by:
Hi,
Everything went fine getting a short date format out of SQL into my
DataGrid with this:
<%# DataBinder.Eval(Container.DataItem, "Created", "{0:d}")%>
Then I got too fancy in SQL and...
|
by: dhnriverside |
last post by:
Hi peeps
I have a datepicker control that's providing dates in the format dd/mm/yyyy
(UK).
I want to convert this to "yyyy-mm-dd" to store as a text field in my
database (had lots of problems...
|
by: Rob T |
last post by:
I'm storing a date/time into a SQL table of type datetime. I need it to be
precise so the value is stored to the 1000th of a second. ie "insert into
myTable mydate values ('08/05/2005...
|
by: johndcal |
last post by:
Hello All,
I have a date value that I pull from a .csv file.
After reading the file and storing the values in an array the value of
the date could
be found in $array, for example....
|
by: David Veeneman |
last post by:
How does one format a date column in a GridView control? I had assumed that
the DataFormat string would do it, but MSDN only shows numeric formatting
codes. Can dates be formatted using that...
|
by: Jim in Arizona |
last post by:
I have a gridview that's being populated from an access db query. The
problem I'm having is that the date/time fields in access that are
populating the gridview are showing both date and time, when...
|
by: Darin |
last post by:
I have an applicatoin that works 100% perfect when running on a machine
setup for English (United States), but when I change it to Spanish
(Mexico), the dates start giving me fits.
THe reason is...
|
by: creative1 |
last post by:
Hello everyone.
I am experiencing a strange problem that I can't fix on my own. I think I need expert's suggestions for this.
The problem is:
I want to print account statement (or any other...
|
by: Martin |
last post by:
I'm retrieving some records from a database. One of the fields
contains a date/time. I would like to format it as I send it out to
the table in the displayed page.
Can some one please tell me...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
| |