I was searching here about converting a string like "16:20" to a DateTime type without losing the format, I said I dont want to add dd/MM/yyy or seconds or AM/PM, because db just accept this format.
I tried with Cultures yet
Thanks in Advance
Just give a date format to your dateTime.
string DateFormat = "yyyy MM d "
this willl give you the year month and day. after continuing;
string DateFormat = "yyyy MM d HH:mm:ss "
in here the Capital H will give you the 24 hours time format
and lowerCase "h" will give you the 12 hours time
format...
when you give the Dateformat as a string you can do whatever you want with date and time.
string DateFormat = "yyyyMMdHHmmss";
string date = DateTime.Now.ToStrign(DateFormat);
OR
Console.writeline(DateTime.Now.ToStrign(DateFormat));
OUTPUT:
20120823132544
All DateTime objects must have a date and a time.
If you want just the time, use TimeSpan:
TimeSpan span = TimeSpan.Parse("16:20");
If you want a DateTime, add that time to the min value:
TimeSpan span = TimeSpan.Parse("16.20");
DateTime dt = DateTime.MinValue.Add(span);
// will get you 1/1/1900 4:20 PM which can be formatted with .ToString("HH:mm") for 24 hour formatting
DateTime.Now.ToString("hh:mm") - If it's C#.
Oh. Only read the header.
DateTime dt = new DateTime(2008, 12, 11, Convert.ToInt32("16"), Convert.ToInt32("32"), 0);
what do you mean by "losing the format".
if you convert it to a DateTime type, then the DateTime object will have dd/mm/yy and other properties. depending on how you plan to use the object, you can "recover" your original settings, by formatting the string output like this: DT.ToString("HH:mm");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With