Solved

Format output from a date time picker

Posted on 2007-12-02
6
1,578 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 142

Expert Comment

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

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 142

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

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

Suggested Solutions

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

930 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

11 Experts available now in Live!

Get 1:1 Help Now