473,320 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Date formatting

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
Oct 9 '07 #1
9 1888
jamesd0142
469 256MB
written in vb. vb.net should be simular...

Expand|Select|Wrap|Line Numbers
  1.  Dim a As String
  2.         a = Mid(System.DateTime.Now.ToString, 1, 10) 'cuts out date value (dd/mm/yyyy)
  3.  
  4.         Dim b As String
  5.         b = Mid(a, 4, 2) 'stores month value (mm)
  6.  
  7.         Dim c As String
  8.  
  9.         If CInt(b) = "01" Then
  10.             c = Mid(a, 1, 2) + "th," + " January " + Mid(a, 7, 4)
  11.         ElseIf CInt(b) = "02" Then
  12.             c = Mid(a, 1, 2) + "th," + " February " + Mid(a, 7, 4)
  13.         ElseIf CInt(b) = "03" Then
  14.             c = Mid(a, 1, 2) + "th," + " March " + Mid(a, 7, 4)
  15.         ElseIf CInt(b) = "04" Then
  16.             c = Mid(a, 1, 2) + "th," + " April " + Mid(a, 7, 4)
  17.         ElseIf CInt(b) = "05" Then
  18.             c = Mid(a, 1, 2) + "th," + " May " + Mid(a, 7, 4)
  19.         ElseIf CInt(b) = "06" Then
  20.             c = Mid(a, 1, 2) + "th," + " June " + Mid(a, 7, 4)
  21.         ElseIf CInt(b) = "07" Then
  22.             c = Mid(a, 1, 2) + "th," + " July " + Mid(a, 7, 4)
  23.         ElseIf CInt(b) = "08" Then
  24.             c = Mid(a, 1, 2) + "th," + " August " + Mid(a, 7, 4)
  25.         ElseIf CInt(b) = "09" Then
  26.             c = Mid(a, 1, 2) + "th," + " September " + Mid(a, 7, 4)
  27.         ElseIf CInt(b) = "10" Then
  28.             c = Mid(a, 1, 2) + "th," + " October " + Mid(a, 7, 4)
  29.         ElseIf CInt(b) = "11" Then
  30.             c = Mid(a, 1, 2) + "th," + " Novermber " + Mid(a, 7, 4)
  31.         ElseIf CInt(b) = "12" Then
  32.             c = Mid(a, 1, 2) + "th," + " December " + Mid(a, 7, 4)
  33.         End If
  34.  
value stored in c
Oct 9 '07 #2
jamesd0142
469 256MB
Written in vb but vb.net should be very similar...

Expand|Select|Wrap|Line Numbers
  1.   Dim a As String
  2.   a = Mid(System.DateTime.Now.ToString, 1, 10) 'cuts out date value (dd/mm/yyyy)
  3.   Dim b As String
  4.   b = Mid(a, 4, 2) 'stores month value (mm)
  5.   Dim c As String
  6.   If CInt(b) = "01" Then
  7.     c = Mid(a, 1, 2) + " January " + Mid(a, 7, 4)
  8.   ElseIf CInt(b) = "02" Then
  9.     c = Mid(a, 1, 2) + " February " + Mid(a, 7, 4)
  10.   ElseIf CInt(b) = "03" Then
  11.     c = Mid(a, 1, 2) + " March " + Mid(a, 7, 4)
  12.   ElseIf CInt(b) = "04" Then
  13.     c = Mid(a, 1, 2) + " April " + Mid(a, 7, 4)
  14.   ElseIf CInt(b) = "05" Then
  15.     c = Mid(a, 1, 2) + " May " + Mid(a, 7, 4)
  16.   ElseIf CInt(b) = "06" Then
  17.     c = Mid(a, 1, 2) + " June " + Mid(a, 7, 4)
  18.   ElseIf CInt(b) = "07" Then
  19.     c = Mid(a, 1, 2) + " July " + Mid(a, 7, 4)
  20.   ElseIf CInt(b) = "08" Then
  21.     c = Mid(a, 1, 2) + " August " + Mid(a, 7, 4)
  22.   ElseIf CInt(b) = "09" Then
  23.     c = Mid(a, 1, 2) + " September " + Mid(a, 7, 4)
  24.   ElseIf CInt(b) = "10" Then
  25.     c = Mid(a, 1, 2) + " October " + Mid(a, 7, 4)
  26.   ElseIf CInt(b) = "11" Then
  27.     c = Mid(a, 1, 2) + " Novermber " + Mid(a, 7, 4)
  28.   ElseIf CInt(b) = "12" Then
  29.     c = Mid(a, 1, 2) + " December " + Mid(a, 7, 4)
  30.   End If
value stored in c
Oct 9 '07 #3
debasisdas
8,127 Expert 4TB
select the following from oracle for the exact format

Expand|Select|Wrap|Line Numbers
  1. select to_char(sysdate,'ddth, month yyyy') from dual
Oct 9 '07 #4
jamesd0142
469 256MB
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...

---------------------------------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. Dim b As String
  2.         b = System.DateTime.Now.Day
  3.         Dim c As String
  4.         c = System.DateTime.Now.Month
  5.         Dim d As String
  6.         d = System.DateTime.Now.Year
  7.         MsgBox(b + " " + getMonth(c) + " " + d)
  8.  
--------------------------------------------------------------------------------------------

AND THEN THE FUNCTION

--------------------------------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1.  Function getMonth(ByVal Val)
  2.         Dim c As String
  3.         If CInt(Val) = "01" Then
  4.             c = "January"
  5.         ElseIf CInt(Val) = "02" Then
  6.             c = "February"
  7.         ElseIf CInt(Val) = "03" Then
  8.             c = "March"
  9.         ElseIf CInt(Val) = "04" Then
  10.             c = "April"
  11.         ElseIf CInt(Val) = "05" Then
  12.             c = "May"
  13.         ElseIf CInt(Val) = "06" Then
  14.             c = "June"
  15.         ElseIf CInt(Val) = "07" Then
  16.             c = "July"
  17.         ElseIf CInt(Val) = "08" Then
  18.             c = "August"
  19.         ElseIf CInt(Val) = "09" Then
  20.             c = "September"
  21.         ElseIf CInt(Val) = "10" Then
  22.             c = "October"
  23.         ElseIf CInt(Val) = "11" Then
  24.             c = "Novermber"
  25.         ElseIf CInt(Val) = "12" Then
  26.             c = "December"
  27.         End If
  28.  
  29.         Return c
  30.     End Function
  31.  
--------------------------------------------------------------------------------------------
Oct 9 '07 #5
Tig201
103 100+
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...
Expand|Select|Wrap|Line Numbers
  1. Dim Str As String, Str1 As String
  2.  
  3. Select Case Right$(Format(Now(), "d"), 1)
  4.   Case Is = "1"
  5.     Str1 = "st"
  6.   Case Is = "2"
  7.     Str1 = "cnd"
  8.   Case Is = "3"
  9.     Str1 = "rd"
  10.   Case Else
  11.     Str1 = "th"
  12. End Select
  13. Str = Format(Now(), "d") & Str1 & ", " & Format(Now(), "mmmm-yyyy")
  14.  
  15.  
It is from VB6
Oct 9 '07 #6
jamesd0142
469 256MB
I modified it like this...

Expand|Select|Wrap|Line Numbers
  1.         Dim b As String
  2.         b = "2" 'System.DateTime.Now.Day
  3.         Dim c As String
  4.         c = System.DateTime.Now.Month
  5.         Dim d As String
  6.         d = System.DateTime.Now.Year
  7.         MsgBox(b + ending(b) + " " + getMonth(c) + " " + d)
  8.  
MsgBox(b + ending(b) + " " + getMonth(c) + " " + d)

and add this function

Expand|Select|Wrap|Line Numbers
  1.   Function ending(ByVal Val)
  2.         Dim a As String
  3.         If CInt(Val) = "01" Then
  4.             a = "st"
  5.         ElseIf CInt(Val) = "02" Then
  6.             a = "nd"
  7.         ElseIf CInt(Val) = "03" Then
  8.             a = "rd"
  9.         Else
  10.             a = "th"
  11.         End If
  12.         Return a
  13.     End Function
  14.  
Oct 9 '07 #7
Killer42
8,435 Expert 8TB
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...

Expand|Select|Wrap|Line Numbers
  1. Public Function Ending(ByVal DayNum) As String
  2.   ' Dim a As String
  3.   Select Case CInt(DayNum )
  4.     Case 1, 21, 31
  5.       Ending = "st"
  6.     Case 2, 22
  7.       Ending = "nd"
  8.     Case 3, 23
  9.       Ending = "rd"
  10.     Case Else
  11.       Ending = "th"
  12.   End Select
  13. 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?
Oct 9 '07 #8
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.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.     Dim b As String
  3.     b = Format(Date, "dd") & Ending(Format(Date, "dd"))
  4.     Dim c As String
  5.     c = Format(Date, "mm")
  6.     Dim d As String
  7.     d = Format(Date, "yyyy")
  8.     MsgBox b & Format(Date, " - mmmm,yyyy")
  9. End Sub
  10.  
  11. Public Function Ending(ByVal DayNum) As String
  12.     Select Case CInt(DayNum)
  13.         Case 1, 21, 31
  14.            Ending = "st"
  15.         Case 2, 22
  16.            Ending = "nd"
  17.         Case 3, 23
  18.             Ending = "rd"
  19.         Case Else
  20.            Ending = "th"
  21.     End Select
  22. End Function
Oct 10 '07 #9
jamesd0142
469 256MB
Great!

This is the problem with me... my code neva looks this neat :P

I tend to use 53 lines where 12 will do :)
Oct 10 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
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...
4
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...
4
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...
12
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...
2
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....
2
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...
3
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...
21
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...
7
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...
9
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
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...
0
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...
0
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...
0
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...
1
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)...
1
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...
1
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....
0
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

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.