Solved

Format output from a date time picker

Posted on 2007-12-02
6
1,580 Views
Last Modified: 2013-12-16
Is there a method for formatting output from a date time picker using the following code
this.textBox1.Text = this.dateTimePicker1.ToString();
I get
System.Windows.Forms.DateTimePicker, Value: 21/11/2007 20:40:07
in the textbox. It would make life easier if it didn't come with the leading string
(Is it possible to select the day, month or year as separate integers (this would be useful for applications that would run predictably in the UK and US)
0
Comment
Question by:johnecollins
  • 3
  • 2
6 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20392305
this.textBox1.Text = this.dateTimePicker1.Value.ToString();
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20392309
note: the .Value is what return the datetime data typed value, and you can "extract" from that the .Year, .Month, .Day
0
 

Author Comment

by:johnecollins
ID: 20392390
angelIII
Thanks for this as you will know already it gave a 'clean' string of the data and time
so for 25th December 2007 I get 25/12/2007 21:18:10 instantly better!
I can then substring the day, month and year
This is a little worrying as in the UK and probably many other places in the world the format of dates is
dd/mm/yyyy
Whereas in the US its mm/dd/yyyy should I refer to 'region'  so that I can determien where the month is?
If that is what is reccomended how do I do it?

Thank you very much

John
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 20392410
please read my second comment:



this.dateTimePicker1.Value.Year
this.dateTimePicker1.Value.Month
this.dateTimePicker1.Value.Day

Open in new window

0
 

Author Closing Comment

by:johnecollins
ID: 31412217
Fine job
angelIII thank you very much for your time and attention
John
0
 

Expert Comment

by:haggard
ID: 20662564
Close, but there's a way to get your date time string formated without going through the process of extracting specific values. The DateTime ToString accepts either a format string or a format string plus a culture specification. If no culture is specified then the date time is displayed in a fashion appropraite for the current application culture. The format string allows specification in an abbreviated or an explicit fashion. For example, date.ToString("d") displays in (for US) M/D/YYYY format. Look here for details on standard datetime formatting.

http://msdn2.microsoft.com/en-us/library/az4se3k1.aspx

Custom formatting gives complete control over everything and here's a link to the details.

http://msdn2.microsoft.com/en-us/library/8kb3ddd4.aspx

==
Richard Lewis Haggard
Haggard-And-Associates.com
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

840 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