How to Truncate Milliseconds Off of a .Net Datetime

How to truncate milliseconds off of a .NET DateTime

The following will work for a DateTime that has fractional milliseconds, and also preserves the Kind property (Local, Utc or Undefined).

DateTime dateTime = ... anything ...
dateTime = new DateTime(
dateTime.Ticks - (dateTime.Ticks % TimeSpan.TicksPerSecond),
dateTime.Kind
);

or the equivalent and shorter:

dateTime = dateTime.AddTicks( - (dateTime.Ticks % TimeSpan.TicksPerSecond));

This could be generalized into an extension method:

public static DateTime Truncate(this DateTime dateTime, TimeSpan timeSpan)
{
if (timeSpan == TimeSpan.Zero) return dateTime; // Or could throw an ArgumentException
if (dateTime == DateTime.MinValue || dateTime == DateTime.MaxValue) return dateTime; // do not modify "guard" values
return dateTime.AddTicks(-(dateTime.Ticks % timeSpan.Ticks));
}

which is used as follows:

dateTime = dateTime.Truncate(TimeSpan.FromMilliseconds(1)); // Truncate to whole ms
dateTime = dateTime.Truncate(TimeSpan.FromSeconds(1)); // Truncate to whole second
dateTime = dateTime.Truncate(TimeSpan.FromMinutes(1)); // Truncate to whole minute
...

How do I truncate milliseconds off Ticks without converting to datetime?

You could use integer division:

if (tick1 / TimeSpan.TicksPerSecond == tick2 / TimeSpan.TicksPerSecond)

This works because if you divide a long/int by a long/int the result is also a long/int therefore truncating the decimal portion.

What is the easiest method to remove the Millisecond's part of a DateTime.UctNow.TimeOfDay?

Everything you need to know about customising the DateTime ToString format is here on MSDN.

In simple terms, something like this:

endDate = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd hh:mm:ss"); 

(alter the format as desired)

subtract datetime.now from a datetime.now and truncat the millisecond

Use this:

var result = string.Format("{0:D2}:{1:D2}:{2:D2}", duration.Hours, duration.Minutes, duration.Seconds);

NHibernate truncate milliseconds from DateTime variables

I got the answer, I added type="Timestamp" to the hbm.xml

<class name="MensagemRecebimento" table="dbo.xxx" lazy="true" dynamic-update="true">

<id name="CodigoRecebimentoMensagem" column="COD_RECB_MSG"> <generator class="identity" /> </id>
<property name="DataReferencia" type="Timestamp"> <column name="DATA_REF_MSG" sql-type="datetime" not-null="true" /> </property>
<property name="DataHoraRecebimento" type="Timestamp"> <column name="DTHR_RECB_MSG" sql-type="datetime" not-null="true" /></property>

</class>

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

Strip seconds from datetime

You can do

DateTime dt = DateTime.Now;
dt = dt.AddSeconds(-dt.Second);

to set the seconds to 0.

Remove hours:seconds:milliseconds in DateTime object

To answer your question, no - you would have to store it in a different type. The most simple choice is to use a string.

string date = dateTime.ToString("MM:dd:yyyy");

However I'd also strongly advise against storing dates internally in your program as strings. This will make it difficult to do any calculations or comparisons on them. Furthermore I'd advise you against forcing a specific culture for your date representation as it means your application probably won't work as expected in other cultures than yours.

A slightly more sophisticated approach is to create a custom class which overrides ToString. I'd also avoid this though, because it will still be difficult to use your type with the standard library functions. You will have to convert back and forth all the time.

Just leave it as a DateTime and do the conversion to string only in the presentation layer. You can use DateTime.ToShortDateStringto print a user friendly culture aware string.



Related Topics



Leave a reply



Submit