Adding days to a DateTime in C# - c#

Is it possible to add dates in C#?
(DateTime.Today.ToLongDateString() + 10)
I tried this but it doesn't work.

Do you want to add days?
DateTime newDate = DateTime.Today.AddDays(10);
Note that you get a new DateTime back!
MSDN

Use DateTime.Today.AddDays(10) or any of the other AddXXX functions on DateTime.

What is the unit of 10. If it is days; then
var todayPlus10Days = DateTime.Today.AddDays(10);

Use AddDays() method:
DateTime dt = DateTime.Today;
dt = dt.AddDays(10);

Related

Get just the time from System.DateTime

Hi I have a local variable of the type of System.DateTime. How can I get just the time? Thanks
DateTime.Now.TimeOfDay returns a TimeSpan representing the time of the day.
If this is for display purposes (As it normally is with these questions), you can simply use one of the following:
myDateTime.ToShortTimeString();
myDateTime.ToString("hh:mm:ss"); //for 12 hour clock
myDateTime.ToString("HH:mm:ss"); //for 24 hour clock
DateTime time = DateTime.Now.TimeOfDay
DateTime dt = DateTime.Now;
string time_now =dt.TimeOfDay.ToString();

DateTime manipulaton in c#

I'm new in C# and I need to understand how create a DateTime object that equal the (DateTime.NOW + 1 hour).
Thanks
Generally speaking, you can Add a TimeSpan value that represents any arbitrary interval to a DateTime. There are helper methods on TimeSpan that help you construct such values, for example:
DateTime.Now.Add(TimeSpan.FromHours(1))
Apart from that, since you only want the simple "+1 hour" you can also use AddHours:
DateTime.Now.AddHours(1)
Try this:
var inOneHour = DateTime.Now.AddHours(1);
DateTime newTime = DateTime.Now.AddHours(1);
You need to use the AddHours function;
DateTime oneHourInTheFuture = DateTime.Now.AddHours(1);
if you are looking to Nullable the DataTime object you could also do something like this
DateTime? newTimeHour;
newTimeHour = new DateTime();
newTimeHour = DateTime.Now.AddHours(1);

c# converting string into date time

I have string like this "24:00:00" and I would like to convert it to time. I tried convert and DateTime.Parse but it seems like it needs a date too. Is there a way to just get time, or do I have to put in a date as well?
If you are only interested in the time component, consider using TimeSpan instead of the full DateTime.
var time = TimeSpan.Parse("23:59:59");
I am not sure "24:00:00" is going to be a valid time. Any how, you should not need to specify the date, you can do...
DateTime time = DateTime.ParseExact("23:59:59", "HH:mm:ss", null);
If your time is actually a time of the day, then I would suggest sticking with DateTime. If you are actually using an amount of time (i.e. can be more that 23:59:59) then you could use TimeSpan...
TimeSpan time = TimeSpan.ParseExact("23:59:59", "HH:mm:ss", null);
don't forget, both have a TryParseExact version if you are not sure you input will be valid
You can use DateTimeFormatInfo to format your DateTime.
string strDate = "23:10:00";
DateTimeFormatInfo dtfi = new DateTimeFormatInfo();
dtfi.ShortTimePattern = "hh:mm:ss";
dtfi.TimeSeparator = ":";
DateTime objDate = Convert.ToDateTime(strDate, dtfi);
Console.WriteLine(objDate.TimeOfDay.ToString());
I think you need TimeSpan.Parse instead?
You can use timespan
http://msdn.microsoft.com/en-us/library/system.timespan.aspx
How about
var time = new DateTime.Today;
var str = "24:00:00";
var split = str.split(":");
time.AddHours(Convert.ToInt32(split[0]));
time.AddMinutes(Convert.ToInt32(split[1]));
time.AddSeconds(Convert.ToInt32(split[2]));
Hope this helps.

How do I subtract one DateTime from another?

I have an instance of DateTime that I get from my database, I want to subtract it from DateTime.Now and find out if 4 hours were passed. How do I do that?
Also when should i use DateTime.UTCNow or DateTimeOffset
You can use the subtraction operator to get a TimeSpan:
private static readonly TimeSpan MinimumTime = TimeSpan.FromHours(4);
...
if ((dateFromDatabase - DateTime.Now) > MinimumTime)
{
...
}
As for whether you need UTCNow or Now... it will depend on what happens to time zones when you fetch the data from the database. DateTime is not terribly clear on this front :(
If you can fetch the value as a DateTimeOffset to start with, then you can use DateTimeOffset.Now instead and it should be simpler to work out any time zone issues.
DateTime.Subtract
First Google hit..
Try this:
bool fourHoursPassed = date.AddHours(4) < DateTime.Now;
or this to actually perform a subtraction:
bool fourHoursPassed = (DateTime.Now - date).TotalHours > 4;
DateTime.Subtract
or
DateTime myDateTime = someValue;
TimeSpan ts = DateTime.Now -myDateTime;
if(ts.Hours>=4)
{
doSomething();
}
Hope it helps.
DateTime dt = new DateTime(2011, 07, 10);
DateTime dob = new DateTime(1987, 07, 10);
You can simply subtract as:
TimeSpan age = dt - dob;

String to time format but how?

I want to change a value from int or string format to datetime format. There is any function in SQL like the following?:
Function: Result
TimeAdd( nextrundate,"sec",45) 00:00:45
TimeAdd( nextrundate,"min",45) 00:45:00
TimeAdd( nextrundate,"hour",4) 04:00:00
But:
TimeAdd( nextrundate,"min",70) 01:10:00
TimeAdd( nextrundate,"min",190) 03:10:00
Is there a method that does this in C# also?
You mean:
TimeSpan.FromSeconds(double)
TimeSpan.FromMinutes(double)
see MSDN: http://msdn.microsoft.com/en-us/library/system.timespan_members(VS.80).aspx
System.TimeSpan s = new TimeSpan();
s.Add(new TimeSpan(days, hours, minutes, seconds, milliseconds))
In SQL you could use something like
convert(varchar(8),dateadd(second,45,nextrundate),114)
convert(varchar(8),dateadd(minute,45,nextrundate),114)
convert(varchar(8),dateadd(hour,4,nextrundate),114)
In C# you can use the DateTime and TimeSpan classes.
DateTime rundate = DateTime.Now();
DateTime nextRunDate= rundate .AddDays(1);
TimeSpan oneDay=(TimeSpan)(nextRunDate-rundate);
There are similar methods for minutes, seconds etc.
DateTime d = new DateTime();
d = d.AddMinutes(70);
Console.WriteLine(d.ToString("yyyy-MM-dd HH:mm:ss"));
DateTime d = new DateTime();
d = d.AddMinutes(70);
Console.WriteLine(d.ToString("yyyy-MM-dd HH:mm:ss"));

Categories