Storing DateTime into Array in c# - c#

I have initialized an array for my DateTime.
DateTime[] departureCalcArray = new DateTime[10];
And then getting the value of the DateTimePicker that is formatted as h:mm tt(that is 8:30 AM with no preceeding zero) .
My code for storing the value of the DateTimePicker is as below.
departureCalcArray[i] = timeDeparture.Value.Date;
However, when I checked if the value is saved via MessageBox.Show();
I keep getting the date today and 12:00:00 AM. Although back in PHP, I use to convert the time to 24hour format so that I can use it in calculation. Any help please?

Change
departureCalcArray[i] = timeDeparture.Value.Date;
to
departureCalcArray[i] = timeDeparture.Value;
When you use the Date property of a DateTime instance, you get a new DateTime instance with the same date, but its Time component set to 12:00AM

I am not able to comment becos I just started this account.
This answer is related to a conversation in comments on how to change to HH:mm
DateTimePicker.ShowUpDown = true;
DateTimePicker.CustomFormat = "hh:mm";
DateTimePicker.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
Credits to here: DateTime Picker In WinForm How To Pick Time?

Related

Parse Time String to DateTime format for HH:MM format of DateTimePicker - C#, WFA

I am trying to take a time string in the format of HH:MM such as 18:30 and turning this into a DateTime string.
I have tried many different methods such as using ParseExact (as seen below), however even when using this code, it still outputs the DateTime string as both the date and the time.
dtpTime.Value = DateTime.ParseExact(Classes.SystemClasses.Booking.getBookingTime(), "H:mm", null, System.Globalization.DateTimeStyles.None);
Which outputs:
20/02/2019 18:56:00
The value in Classes.SystemClasses.Booking.getBookingTime() is 18:56 which is the value I wish to enter into a DateTimePicker on a form in the format of HH:MM
Any help to resolve this problem would be greatly appreciated and if the explanation is not clear enough, please feel free to ask myself any questions.
Thanks, Ryan.
What you are trying to do is hold a TIME inside a DateTime variable. That is not possible, as the name suggest, this type is used to hold a Date and a Time. If you need only the Time part of the date, you need to convert it to a string:
var time = DateTime.ParseExact(Classes.SystemClasses.Booking.getBookingTime(), "HH:mm", null, System.Globalization.DateTimeStyles.None).ToString("HH:mm");

Display 01/01/1999 instead of 01/01/1999 00:00:00 in TextBox

i have a TextBox which displays this format '01/01/1999 00:00:00' but my record has a date type not a datetime.
Thank you
my recors have a date type not datetime
.Net DateTime has Date and Time together. If your record has only date then you will see the time set at 00:00:00. To display only date you can use:
textBox.Text = yourDateObject.ToShortDateString();
Or you can pass custom format to ToString like:
textBox.Text = yourDateObject.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
For more info see: Custom Date and Time Format Strings

How can i change a date string format

I'm returning from Facebook Graph a friend birthday in MM/DD/YYY format. I need to show it in DD/MM/YYY, cause it's the brazil date format. I'am returnin a M/D string. I cant change the datetime format. How can I only display D/M for the user without changing datetime? Only manipulating the returning string?
The error that you're receiving tells me that your birthday variable is a string. To format it, you can first convert it to a DateTime (it's probably the simplest way).
var birthdayString = "06/28/2013";
var date = Convert.ToDateTime(birthdayString);
date.ToString("dd/MM - MMMM d"); // 28/06 - June 28
date.ToString("dd/MM/yyyy"); // 28/06/2013
Like I said in the comments above, you can check out more about formatting dates here:
Custom Date and Time Format Strings
Depending on the format just take your date to string.
birthdaydate.ToString("dd/MM/YYYY"); //10/01/1970
birthdaydate.ToString("dd MMMM"); // 10 January
birthdaydate.ToString("MM/dd/YYYY"); //01/10/1970
and so on. please refer to http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx for further information
If you don't want to input the format manually, you can do this instead:
string dateString = myDateTime.ToString(new CultureInfo("pt-BR"));

MVC C# DateTime formatting fix needed

As you can see in the below screen shot. I have Date which is 7/12/2011 12:00:00 AM. Date is described wrong even if I format it. 7 should be the day and 12 is the month.
How I fix that to get proper formatting for yellow return string?
In the below screen shot the Date is 28/12/2011 11:00 where 28 is day and 12 is month. Trying to convert that string into DateTime to save into SQL Server DateTime field but gives conversion problem. Anyone tell me why is that and How to fix it?
Solution:
I solved problem like below. When I want saving date in SQL Server 2008 r2 the default was saved like 2011-08-12 11:00:00.000 which was causing problem. I changed that formatting Date when it was going to be saved in SQL like below and it worked
DateTime n = Convert.ToDateTime(start_date);
var h = String.Format("{0:dd/MM/yyyy}", n);
if (start_date != "")
{
changedEvent.start_date = Convert.ToDateTime(h);
}
Output now is 2011-12-08 11:00:00.000. Do you think any clean work around?
You should call DateTime.ParseExact(start_date, "dd/MM/yyyy", CultureInfo.InvariantCulture)
Try:
DateTime.ParseExact(str, "dd/MM/yyyy HH:mm:ss TT", null); //28/12/2011 11:00:00 AM
DateTime.ParseExact(str, "dd/MM/yyyy HH:mm", null); //28/12/2011 11:00
I think you are addressing the wrong problem. If you want DateTime to recognize your locale date format, then you should make sure the servers date locale is set for your local one. Then, DateTime will convert the date correctly without conversion.
If that's not possible (say you're using a shared server in a different locale) then the ParseExact method would be one solution, but it will only fix some of the problem. For instance, dates posted and model bound will attempt to parse in the servers locale format.
You may need to set your locale explicitly, using something like this:
Thread.CurrentThread.CurrentUICulture = new CultureInfo("es-MX");

How to set datetimepicker value to only date in (.NET)

I have DateTimePicker on my form and I set a value to the custom format property to "dd/MM/yyyy" ant when I run this code:
MessageBox.Show(dateTimePicker1.Value.ToString());
I get this value : "3/26/2010 1:26 PM".
How I can remove the time part from value.
I know we can use this method
dateTimePicker1.Value.ToShortDateString();
but I want to set the value property to this format "dd/MM/yyyy" so the output will be like this "26/3/2010", because I want to store the value in my DB (SQL)
How I can do that?
Use dateTimePicker1.Value.Date to get the Date part of this DateTime value.
Do notice though, if you mean to parse into strings, that using dateTimePicker1.Value.Date.ToString will result with the "26/03/2010 00:00:00" string, while using something like MyString = CStr(dateTimePicker1.Value.Date) will result in MyString being "26/03/2010".
If you have string as datatype in database:
dateTimePicker1.Value.Date.ToString("d");
If datatype is DateTime:
dateTimePicker1.Value.Date;
Both will return date in dd/mm/yyyy format without time.
I assume you initialized the DateTimePicker with the current date and time:
dateTimePicker1.Value = DateTime.Now;
Instead, initialize it with the current date:
dateTimePicker1.Value = DateTime.Today;
What happens is as follows:
If the user selects a date, then the DateTimePicker.Value property will always return a date with no time of day component, and DateTime.Kind set to Unspecified.
If the user doesn't select a date, then the DateTimePicker.Value property will return the value used to initialize the DateTimePicker.Value property. If you initialize it with DateTime.Now, it will have a time of day component, and DateTime.Kind will be set to Local.
just MessageBox.Show(dateTimePicker1.Value.ToString("dd/MM/yyyy"));
dateTimePicker1.Value.ToString("dd-MM-yyyy")
This should work
You can set DateTimePicker "Format" to "Short" in Properties window (under Appearance)

Categories