Storing current time without milliseconds component
Storing current time without milliseconds component
I have a timespan object that needs to hold only time, without date. I would use
DateTime.Now.TimeOfDay
but the problem is it gives time in the format
15:51:51.7368329
I don't want the milliseconds component. How can I trim it out?
Answer by Mohib Sheth for Storing current time without milliseconds component
You can either use DateTime.Now.Hour/Minute/Second
properties or you could use DateTime.Now.ToString("HH:mm:ss")
.
Refer here for more info: http://msdn.microsoft.com/en-us/library/zdtaw1bw.aspx
Answer by Sasha Reminnyi for Storing current time without milliseconds component
- When displaying to user you can specify needed format. Here is a good tutorial:
http://www.geekzilla.co.uk/View00FF7904-B510-468C-A2C8-F859AA20581F.htm
- You can create new DateTime object passing to constructor only hour, minute, second (it's for saving.)
Answer by Mark Avenius for Storing current time without milliseconds component
If the problem is displaying it, you can do this:
DateTime.Now.ToString("HH:mm:ss")
Answer by Anders Abel for Storing current time without milliseconds component
Simply subtract away the millisecond part:
DateTime myTime = DateTime.Now.TimeOfDay; myTime = myTime.AddMilliseconds(-myTime.Millisecond);
It could be done in less code, without first assigning to myTime
:
DateTime myTime = DateTime.Now.TimeOfDay.AddMilliseconds( -DateTime.Now.TimeOfDay.Millisecond);
Although somewhat elegant, it is a bad idea. When accessing TimeOfDay
twice, there is a chance that it at some point will have passed another millisecond before the second access. In that case the result would not be zero milliseconds.
Answer by mrcrowl for Storing current time without milliseconds component
I believe this is what you may be after:
TimeSpan timeNow = DateTime.Now.TimeOfDay; TimeSpan trimmedTimeNow = new TimeSpan(timeNow.Hours, timeNow.Minutes, timeNow.Seconds);
Answer by Sujeet for Storing current time without milliseconds component
You can use this function to check what format suits you:
DateTime.Now.GetDateTimeFormats();
This will give you all the Formats like:
"14/05/2011"
"14/05/11"
"14.05.11"
"14-05-11"
"2011-05-14"
- etc.
Answer by Abrar Jahin for Storing current time without milliseconds component
You can do this-
DateTime.Parse( DateTime.Now.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss"), System.Globalization.CultureInfo.CurrentCulture );
Worked for me :).
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment