Set Time Portion of a Datetime Variable

Set time portion of a datetime variable

DECLARE @start_date DATETIME
DECLARE @end_date DATETIME

SET @start_date = DATEADD(hour, 20, DATEDIFF(DAY, 2, GETDATE()))
SET @end_date = @start_date + 1

select @start_date, @end_date

How to set only time part of a DateTime variable in C#

Use the constructor that allows you to specify the year, month, day, hours, minutes, and seconds:

var dateNow = DateTime.Now;
var date = new DateTime(dateNow.Year, dateNow.Month, dateNow.Day, 4, 5, 6);

How do I change a DateTime variable's time?

The DateTimetype is immutable - all the properties are read-only, so you cannot set the Date, Time or even a part of them (years, mins etc).

Since none of the properties can be set directly, to change any part of one use one of the methods to AddSeconds(), AddYears() etc. These all create and return a new DateTime value with the desired adjustment. To subtract, just use a negative value:

WorldDate = WorldDate.AddMinutes(unit)
' to subtract:
WorldDate = WorldDate.AddMinutes(-unit)

The result must be assigned, either to a new variable or the existing one.

The reason is pretty simple: adding n minutes might roll over the Day or Month as well as other properties like DayOfYear or DayOfWeek. Allowing direct access would require lots of checking that the value is legal (like .Minutes = 67 or .Day = 33).

To set the date or time to specific values, create a new one using some of the old values and specifying the other values you want:

WorldDate  = New DateTime(WorldDate.Year, WorldDate.Month, WorldDate.Day, 
myHrs, mySecs, myMins)

Set time part of datetime variable to 18:00

SELECT DATEADD(hh, 24 * 2 + 18, DATEADD(dd, DATEDIFF(dd, 0, GETDATE()), 0))

This truncates the current date and adds 2 days and 18 hours to it (24 * 2 + 18).

A possible variation:

SELECT DATEADD(hh, 18, DATEADD(dd, DATEDIFF(dd, -2, GETDATE()), 0))

How to change time in DateTime?

You can't change a DateTime value - it's immutable. However, you can change the variable to have a new value. The easiest way of doing that to change just the time is to create a TimeSpan with the relevant time, and use the DateTime.Date property:

DateTime s = ...;
TimeSpan ts = new TimeSpan(10, 30, 0);
s = s.Date + ts;

s will now be the same date, but at 10.30am.

Note that DateTime disregards daylight saving time transitions, representing "naive" Gregorian time in both directions (see Remarks section in the DateTime docs). The only exceptions are .Now and .Today: they retrieve current system time which reflects these events as they occur.

This is the kind of thing which motivated me to start the Noda Time project, which is now production-ready. Its ZonedDateTime type is made "aware" by linking it to a tz database entry.

How to update time part in datetime variable

You could apply DATEADD three times.

DECLARE @myDate DateTime = '2019-01-09'
SELECT @myDate
SELECT @myDate = DATEADD(SECOND, 59, DATEADD(MINUTE, 59, DATEADD(HOUR, 17, @myDate)))
SELECT @myDate

This produces

2019-01-09 00:00:00.000

2019-01-09 17:59:59.000

Can we add date and time values to a DateTime variable separately?

How about adding the TimeOfDay part to the Date part using DateTime.Add()?

var date = new DateTime(2018, 1, 5, 20, 0, 0);
var time = new DateTime(2018, 3, 2, 15, 0, 0);

DateTime combined = date.Date.Add(time.TimeOfDay);

Results in

1/5/2018 3:00:00 PM

How to change only the date portion of a DateTime, while keeping the time portion?

With the information you have given, I think this method is fine. If you want to avoid rewriting the oldDateTime.Hour,oldDateTime.Minute,0 piece often, you could create your own static class to simplify the method calls.

In your regular application:

class Program
{
static void Main(string[] args)
{
DateTime time = DateTime.Now;
DateTime newDateTime = MyDateTimeUtil.CreateDateFromTime(2015, 12, 12, time);
}
}

The static class that creates the DateTime value:

public static class MyDateTimeUtil
{
public static DateTime CreateDateFromTime(int year, int month, int day, DateTime time)
{
return new DateTime(year, month, day, time.Hour, time.Minute, 0);
}
}


Related Topics



Leave a reply



Submit