Solved

Format date in string

Posted on 2007-11-25
5
234 Views
Last Modified: 2011-01-13
I am applying content of a date field to a text field as a string, on a from. Data in the date field is in the long date format e.g. 7/20/2007 12:07:00 PM. I do not want the time information (12:07:00 PM) in the text field. How can I eliminate it from the string? Cannot change the date format in the underlying table, as it is transmitted data. Changing the date format (to short date) on the form did not help.
0
Comment
Question by:ChuckRaine
5 Comments
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 25 total points
ID: 20345732
a datetime variable has a ToString() method, which can accept a format parameter.
http://articles.techrepublic.com.com/5100-3513-6106789.html
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 25 total points
ID: 20345770
having the date/time stored in a string, you can extract the date in many ways:

Dim justdate as String
justdate = fulldate.Substring(0, fulldate.IndexOf(" "))
0
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 50 total points
ID: 20345833
> Data in the date field is in the long date format

That is not completely correct. The data in the field doesn't have a format at all. When you convert the data to a string in order to display it, the conversion uses a specific format.

If you don't specify any format for the conversion, the date format of the current culture is used. You can specify the format in serveral ways, and use different ways of converting the value, depending on the format you want. Some examples:
theField.Text = theDate.ToShortDateString()

theField.Text = theDate.ToLongDateString()

theField.Text = theDate.ToString("d") 'short date

theField.Text = theDate.ToString("D") 'long date

theField.Text = theDate.ToString("M/d/yyyy") 'custom format

theField.Text = String.Format("{0:d}", theDate)

theField.Text = String.Format("{0:D}", theDate)

theField.Text = String.Format("{0:M/d/yyyy}", theDate)

Open in new window

0
 
LVL 11

Assisted Solution

by:AkisC
AkisC earned 25 total points
ID: 20350262
Another suggestion ...
        Dim theDate As Date = Now()
        MsgBox(theDate)
        theDate = DateSerial(Microsoft.VisualBasic.DateAndTime.Year(theDate), Microsoft.VisualBasic.DateAndTime.Month(theDate), Microsoft.VisualBasic.DateAndTime.Day(theDate))
        MsgBox(theDate)
0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 20351214
@AkisC:

Are you kidding? I don't think that I have seen a more complicated way of writing:

theDate = theDate.Date

;)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now