Omitting the Milliseconds in a Date

Omitting the Milliseconds in a Date

If you don't want to use string conversions, here's a solution:

DECLARE @TheDate datetime, @Today datetime
SET @TheDate = GetDate()

SET @Today = DateAdd(dd, DateDiff(dd, 0, @TheDate), 0)
SELECT DateAdd(s, DateDiff(s, @Today, @TheDate), @Today)

SQL Server remove milliseconds from datetime

You just have to figure out the millisecond part of the date and subtract it out before comparison, like this:

select * 
from table
where DATEADD(ms, -DATEPART(ms, date), date) > '2010-07-20 03:21:52'

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
...

Remove Seconds/ Milliseconds from Date convert to ISO String

While this is easily solvable with plain JavaScript (see RobG's answer), I wanted to show you the Moment.js solution since you tagged your questions as "momentjs":

moment().seconds(0).milliseconds(0).toISOString();

This gives you the current datetime, without seconds or milliseconds.

Working example: http://jsbin.com/bemalapuyi/edit?html,js,output

From the docs: http://momentjs.com/docs/#/get-set/

DateTime serializers omitting milliseconds?

JSON.NET and System.Text.Json is breaking the specification, because we are sending different 'formats'?

JSON isn’t an “adopting standard” as it does not reference ISO 8601, or prescribe any particular format for date and time. In JSON they are just strings. So serializers are free to represent dates in any way they like. Both serializers choose ISO 8601, and fractional seconds are not required and often useless extra bytes.

This is the downside of JSON being radically simple.

System.Text.Json has custom converters you can use to override the default behavior of the serializer: How to write custom converters for JSON serialization (marshalling) in .NET

How to ignore milliseconds and format using DateTimeFormatter

You are reinventing the ISO_ZONED_DATE_TIME.

For this format, milliseconds are already optional. You only have to truncate them when present, to get your desired result. The format is even the standard, so the code is as simple as:

String[] samples = {
"2020-10-01T12:05:22.458-04:00",
"2020-10-01T12:05:22-04:00",
};
for(String s: samples) {
ZonedDateTime parsed = ZonedDateTime.parse(s).with(ChronoField.MILLI_OF_SECOND, 0);
System.out.println(parsed);
}
2020-10-01T12:05:22-04:00
2020-10-01T12:05:22-04:00

Note that I used MILLI_OF_SECOND to make the solution more intuitive regarding your task of removing the milliseconds. Actually, milliseconds are just nanoseconds with a lower precision, so you could use the even simpler ZonedDateTime.parse(s).withNano(0), once it has been understood.

Or, as suggested by Ole V.V., ZonedDateTime.parse(s).truncatedTo(ChronoUnit.SECONDS), which might be even easier to grasp.

simple way to drop milliseconds from python datetime.datetime object

You can use datetime.replace() method -

>>> d = datetime.datetime.today().replace(microsecond=0)
>>> d
datetime.datetime(2015, 7, 18, 9, 50, 20)

Ignore seconds and milliseconds FROM GETDATE() in SQL

I'd either use the DATEADD/DATEDIFF trick that Codo has shown or just cast it to smalldatetime1:

select CAST(GETDATE() as smalldatetime)

I'd avoid anything that involves round-tripping the value through a string.

1It may be appropriate, at this time, to change your schema to use this data type anyway, if seconds are always irrelevant.

JavaScript - remove milliseconds from date object

Try to split your toISOString() with . using String.prototype.split(), grab the 0th index and concatenate the literal Z with the output. This way you can simply ignore the 1st index that contains the milliseconds value. Hope this helps :)

var sun = new Date();sun.setDate(sun.getDate() + (7 - sun.getDay()));sun.setHours(9);sun.setMinutes(0);sun.setSeconds(0);console.log(sun.toISOString().split('.')[0]+"Z");


Related Topics



Leave a reply



Submit