Solved

Format date in string

Posted on 2007-11-25
5
232 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
Comment Utility
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
Comment Utility
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
Comment Utility
> 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
Comment Utility
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
Comment Utility
@AkisC:

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

theDate = theDate.Date

;)
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

744 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

13 Experts available now in Live!

Get 1:1 Help Now