How to display time from DateTime field in DateTimePicker in C#? - c#

Lots of troubles with DateTime fields today...
I have a DateTime field that I want to separate and display as a date in one DateTimePicker and a time in another DTP. This is what I tried:
dtp_date.Value = myRow.myDateTime.Date;
dtp_time.Value = myRow.myDateTime.TimeOfDay;
I'm getting an error that says it can't convert type 'System.TimeSpan' to 'System.DateTime'. I guess I just don't understand what I need to do differently. All it will do is display the time of day right now, not the one stored in my DateTime field.
Anyone know how to extract the time?

Try giving the DateTimePicker a valid date AND time. Hide the date on display and only show the time using DateTimePickerFormat.Time
DateTimePicker1.Value = new DateTime( DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 5, 30, 0 )
Pop in your time values from the myRow.myDateTime field

You could set the CustomFormat property of your DateTimePicker control:
dtp_date.CustomFormat = CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern;
dtp_date.Value = myRow.myDateTime;
That should show the time portion of your date.

Just create a New DateTime object with the values you need:
dtp_time.Value = new DateTime(0,0,0,myRow.myDateTime.Hour,myRow.myDateTime.Minute, myRow.myDateTime.Second);

Related

how to get the day from the date in sql and C#?

Does anyone know how to get the day part from a datetime? For example, I have a datetime 2013-11-30 11:53:02.360. I just need the 30 from the datetime. How do I get it using sql and C#?
Update: Maybe I didn't explain it right. Here is the scenario:
We have a record in our table which has start date and end date, for example:
2013-10-03 11:53:02.360
2013-10-08 14:36:01.090
In my web page I have an event that only occurs if the current day is between 3-8, I don't care about the year and month. So if the current date is 2014-01-05, the event will occur.
Now I question is, if I have 2013-09-30 ... as start date in the table, how do I check to make sure the event will still occur? Basically I think I need to check current date is fall into the same rage of the dates defined in the table.
DateTime.Parse("2013-11-30 11:53:02.360").Day
In T-SQL (assuming you're talking about SQL Server when you say SQL):
SELECT DAY(GETDATE())
In C#:
int day = DateTime.Today.Day;
if your date Type is DateTime and contains 2013-11-30 11:53:02.360
Try This:
String Day=dt.ToString("dd"); //it contains Day-> 03
( or )
if your date Type is String and contains 2013-11-30 11:53:02.360
Try This:
String strDate = "2013-11-03 11:53:02.360";
DateTime dt = DateTime.ParseExact(strDate, "yyyy-MM-dd HH:mm:ss.fff",
System.Globalization.CultureInfo.InvariantCulture);
String Day=dt.ToString("dd"); //it contains Day-> 03

How to set the time in date timepickers in c#

I'm new to date timepickers. I was wondering if someone could explain them to me properly. I have a booking system that you must be able to book a date and specific time in the future and reserve that time frame in a sql database. Can I do it with date timepickers? It has a nice interface when its on my form but I cant see a way for the user to set the time? it always just gives me the default time. Any help? thanks
http://imgur.com/arZPxA5
The DateTimePicker-Control has a Format-Property, which will set the format of the date and time displayed in the control.
AS stated in this answer you can use following format to show Date and time:
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "MM.dd.yyyy hh:mm:ss";
Here you can get a list of all format string and their descriptions:
List of CustomFormat-String
The result will look like this:
.NET also has a structure called DateTime to save your selected value.
You can save it with following code
DateTime yourSelectedDateTime = dateTimePicker1.Value;
textBox1.Text = yourSelectedDateTime.ToString();
Date Time pickers are pretty useful to use when you want people to be able to visually select a date/time. once the user selects a datetime, you can access it like this
int mymonth = this.datetimepicker1.Value.Month
int myday = this.datetimepicker1.Value.day
etc...
Furthermore, you can use events to trigger when stuff happens with them. So for instance, they select a date, you can use the ValueChanged event to make other fun stuff happen too.

GETDATE() always returns 12:00:00 AM

In my ASP.NET (C#) project, I'm inserting some data into a database.
INSERT INTO USERS (username,join_date) VALUES('Ali',GETDATE());
Now when I'm fetching the date from the database, I'm using DateTime.
SqlDataReader r = Command.ExecuteReader();
r.Read();
myDate = r.getDateTime(0);
And when I'm inserting this in some DIV, I do this.
"<div>"+myDate.ToLongTimeString()+"</div>"
I get the correct date as in day/month/year, but the hour is always 12:00:00 AM.
How can I get the exact time?
make sure join_date is of type DATETIME and not DATE in the database
http://msdn.microsoft.com/en-us/library/ms186724.aspx
Using DATE as your column type means you are only storing the date not the time.
Therefore when you read the value back into a DateTime object the system has to initialise the time part - to midnight.
Convert the column to DATETIME and you'll get the time component stored as well.
For more information on the difference see the MSDN page on Date and Time Data Types and Functions

How to get the Time value from datetime picker

How to get the time value in a stored procedure from a DateTimePicker value?
For Example:
I have a datetime picker value stored into my table as:
'2010-04-06 09:00:00.000'
'2010-04-07 14:30:00.000'
I have to get the date seperately using Convert(varchar,Date,3) as Date
I have got the time seperately using Convert(time,Time)
But it show the values as 9:00 or 14:30
I need it to show them as 9:00AM and 2:30PM instead of 14:30.
You can use yourDate.ToString("t");.
Also check other formats
try something like that:
date.ToString("hh:mm tt", CultureInfo.InvariantCulture);
to get current time you can use
now.toshorttime()

How can I set a DateTimePicker control to a specific date?

How can I set a DateTimePicker control to a specific date (yesterday's date) in C# .NET 2.0?
Just need to set the value property in a convenient place (such as InitializeComponent()):
dateTimePicker1.Value = DateTime.Today.AddDays(-1);
If you want to set a date, DateTimePicker.Value is a DateTime object.
DateTimePicker.Value = new DateTime(2012,05,28);
This is the constructor of DateTime:
new DateTime(int year,int month,int date);
My Visual is 2012
You can set the "value" property
dateTimePicker1.Value = DateTime.Today;
Can't figure out why, but in some circumstances if you have bound DataTimePicker and BindingSource contol is postioned to a new record, setting to Value property doesn't affect to bound field, so when you try to commit changes via EndEdit() method of BindingSource, you receive Null value doesn't allowed error.
I managed this problem setting direct DataRow field.
This oughta do it.
DateTimePicker1.Value = DateTime.Now.AddDays(-1).Date;
Use the Value property.
MyDateTimePicker.Value = DateTime.Today.AddDays(-1);
DateTime.Today holds today's date, from which you can subtract 1 day (add -1 days) to become yesterday.
DateTime.Now, on the other hand, contains time information as well. DateTime.Now.AddDays(-1) will return this time one day ago.
Also, we can assign the Value to the Control in Designer Class (i.e. FormName.Designer.cs).
DateTimePicker1.Value = DateTime.Now;
This way you always get Current Date...
dateTimePicker1.Value = DateTime.Today();
FYI: If you are setting the value, and not seeing anything - you might check to see if you have a 'CustomFormat' set - I just hit this and it was set to ' ' for the 1/1/1900 value (our 'not set' value) and set to MM/dd/yyyy if not.

Categories

Resources