Solved

VB.NET DateTimePIcker Time ONLY

Posted on 2014-07-24
10
4,899 Views
Last Modified: 2014-07-24
I am trying to use only the time portion of a DateTimePicker.
I want to populate multiple DateTimePickers with different times

From what I've seen, I have to set a date as well as a time, even though I have the format set to Time.

DateTimePicker0.Value = "7/24/2014 9:01 AM"

Is there a way to set only the time?
something like:
DateTimePicker0.Value = "9:01 AM"

thanks,
0
Comment
Question by:Adam_930
  • 4
  • 3
  • 3
10 Comments
 
LVL 15

Expert Comment

by:Ess Kay
ID: 40216967
A snippet out of the mdsn

'The following code sample shows how to create a DateTimePicker that enables users to choose a time only.'

timePicker = new DateTimePicker();
timePicker.Format = DateTimePickerFormat.Time;
timePicker.ShowUpDown = true;

Open in new window

http://msdn.microsoft.com/en-us/library/ms229631.aspx
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40216999
Please see the following Microsoft documentation for using a DateTimePicker for Time only, How to: Display Time with the DateTimePicker Control.
0
 
LVL 15

Expert Comment

by:Ess Kay
ID: 40217003
If you want to set the time, avoid null values by using todays date

DateTimePicker1.Value = new DateTime( DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 5, 30, 0 )

Open in new window



If you want to preserve the date but only change the time use this
Dim Hour = 1
Dim Minute = 30
Dim Second = 5
DateTimePicker1.Value = new DateTime(DateTimePicker1.Value.Year, DateTimePicker1.Value.Month, DateTimePicker1.Value.Day, Hour, Minute, Second )

Open in new window

0
 

Author Comment

by:Adam_930
ID: 40217006
esskayb2b:
I have done what you have suggested, but I want to populate the value of the time only from within code

DateTimePicker0.Value = "7/24/2014 7:01 AM"

Do I have to put in a date (even if fictitious) and then strip it back out when I execute my application?

What I was hoping to do is something like:  DateTimePicker0.Value = "7:01 AM" because I am only dealing with time
0
 

Author Comment

by:Adam_930
ID: 40217031
I am trying to understand how the .NET DateTimePIcker functions,, eventually the information for the DateTimePIcker will get it's information from a file that read when the user brings up the screen
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 100 total points
ID: 40217033
The backing store for DateTimePicker0.Value is a DateTime object and therefore can not be set with just a Time element.
0
 

Author Comment

by:Adam_930
ID: 40217040
Is there a better control to use than DateTimePIcker if I am dealing with just time?
0
 
LVL 15

Expert Comment

by:Ess Kay
ID: 40217046
0
 
LVL 15

Accepted Solution

by:
Ess Kay earned 150 total points
ID: 40217061
A workaround would be to have the datestring somewhere, and just add that plus time

IE
dim foobar = "7/24/2014 "
DateTimePicker0.Value = foobar & "7:01 AM"
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40217086
Or you could roll your own by inheriting from DateTimePicker and adding code to behave the way you need it to.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

762 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