This question already has an answer here:
DateTime losing Milliseconds
(1 answer)
Closed 5 years ago.
DateTime x = DateTime.ParseExact("2017_10_16 13:52:03.112","yyyy_MM_dd HH:mm:ss.fff", null);
Console.WriteLine(x);
"10/16/2017 1:52:03 PM"
It gives output as "10/16/2017 1:52:03 PM"
Here I am missing the milliseconds 112.
I have looked into many online posts but it does not work for me.
What you have is correct. Only Console.WriteLine(x); does not show you milliseconds by default.
If you ask for this: Console.WriteLine(x.ToString("fff")); you will see your milliseconds
Related
This question already has answers here:
Can you round a .NET TimeSpan object?
(10 answers)
Closed 6 years ago.
Round timespan seconds I also need do same with .Hours and .Minutes
This:
1.53994 second
to:
1 second
This code worked for me.
TimeSpan ts = TimeSpan.FromSeconds(1.54);
Math.Round(ts.TotalSeconds);
This question already has answers here:
How do I get the AM/PM value from a DateTime?
(15 answers)
Closed 6 years ago.
I have the value as #7/13/2016 3:20:00 PM# And want to separate it out date and time. Format for date is "07/13/2016" and time is "03:20 PM". I have got the values with StartDateTime.ToString("MM/dd/yyyy") and StartDateTime.ToString("HH:mm") but i am not sure about the "AM or PM" thing.
If I understand correctly, you have a DateTime value that you are trying to break into separate Date and Time. Microsoft has great documentation on DateTime formatting but I think what you are looking for for your time element is this:
SomeDateTime.ToString("hh:mm tt");
which should output "03:20 PM" or whatever the case may be.
This question already has answers here:
Calculate difference between two dates (number of days)?
(17 answers)
Closed 9 years ago.
I want to calculate no of days remaining for particular day from today onwards. How to calculate difference between 2 dates in C#??
(Note: This answer was written when the question was worded very differently)
Assuming your dates are DateTime or DateTimeOffset:
TimeSpan delta = date1 - date2
TimeSpan GetDelta(DateTime d1, DateTime d2)
{
return (d1 - d2).TotalDays;
}
This question already has answers here:
C# String Format for hours and minutes from decimal
(3 answers)
Closed 9 years ago.
If I have a double like 2.75, is there a way in .Net to format it as '2:45'
If it is for example, 2.75555555555, it should round it to the nearest minute.
I would not mind coding this myself, but I am wondering if .Net can. I checked ToString but did not find anything.
Thanks
Use TimeSpan and its ToString formatter:
TimeSpan timespan = TimeSpan.FromHours(2.75);
string output = timespan.ToString("h\\:mm");
For example
TimeSpan.FromHours(2.75555).ToString("h\\:mm")
outputs
2:45
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
convert string in a text box to dd/mm/yyyy date
When I try to parse
DateTime.ParseExact("22/11/2012", "dd/mm/yyyy", CultureInfo.InvariantCulture).ToString();
I got
1/22/2012 12:11:00 AM
I want to return back to exactly the same origin date which is 22/11/2012.
use "dd/MM/yyyy" mm - minutes, MM - month
check patterns here.
The month is actually referenced with 'MM'. Documentation