This question already has answers here:
how get yesterday and tomorrow datetime in c#
(9 answers)
Closed 5 years ago.
I have two variables
startdate
enddate
In C#, I want to specify in variables startdate and enddate where the date is equal to yesterday.
For example result I want to achieve:
currentday = '14.09.2017'
startdate = '13.09.2017'
enddate = '13.09.2017'
Try this:
System.DateTime today = System.DateTime.Now; System.DateTime yesterday = today.AddDays(-1);
How about
startdate = DateTime.Now.AddDays(-1)
Doesn't that give you exactly what you want?
You have to use DateTime.Now.Date.AddDays(-1) for yesterday and DateTime.Now.Date.AddDays(1) for tomorrow.
Related
This question already has answers here:
Date Difference in ASP.Net
(6 answers)
Closed 3 years ago.
I organized a program which started on 31st December 2018 at 10:00pm hence its been four months ago, i want a way to find this duration by code.
for example , how youtube is able to tell when a comment was written(eg,4years ago,5 months ago).
You can simply substract a DateTime object from another, which results in a TimeSpan representing the difference:
DateTime x = DateTime.Now;
DateTime y = DateTime.Today;
TimeSpan difference = x - y;
var programStartDateTime = new DateTime(2018, 12, 31);
var timeSpan = DateTime.Now - programStartDateTime;
Console.WriteLine($"The difference is: {timeSpan.ToString()}");
I think below sample code may help you
DateTime date1 = DateTime.Now;
DateTime date2 = DateTime.Now.AddDays(-1);
TimeSpan time = date1 - date2;
WriteLine($"TimeSpan : {time}" );
This question already has answers here:
How to subtract one year from DateTime [closed]
(4 answers)
Closed 8 years ago.
I am trying to find a date (string) that is 5 years from current date in the past:-
DateTime.UtcNow.Date - DateString = 5 years
Can anyone help me formulate this into c# syntax?
var myDate = DateTime.UtcNow;
var newDate = myDate.AddYears(-5);
You can add or subtract a TimeSpan from a DateTime object to get another DateTime object.
var fiveYearsAgo = DateTime.Now - TimeSpan.FromYears(5);
var fiveYearsFromNow = DateTime.Now + TimeSpan.FromYears(5);
This question already has answers here:
Converting a String to DateTime
(17 answers)
Closed 8 years ago.
I have string with datetime format dd/MM/yyyy hh:mm.I want to calculate duration between two dates but failed to get datetime in correct format.please help.
thanks in advance
After parsing date string create two dates.
DateTime date1 = new DateTime();
DateTime date2 = new DateTime();
date1 = DateTime.Parse("22/05/2013 09:50:00");
date2 = DateTime.Parse("22/05/2014 09:50:00");
Then use TimeSpan structure to calculate interval:
TimeSpan ts_interval = date2 - date1;
You can use the following properties:
ts_interval.TotalSeconds;
ts_interval.TotalMinutes;
ts_interval.TotalHours;
For more visit http://msdn.microsoft.com/en-us/library/system.timespan_properties%28v=vs.110%29.aspx
you can use build in method
DateTime.Parse("12/05/1999 18:25");
you can also check this post
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Calculate previous week's start and end date
I need to get the previous week start date in c#
for eg. today is Jan 9th.
previous week start date is Jan 1th.
I am using the following code
DayOfWeek weekStart = DayOfWeek.Sunday;
DateTime startingDate = DateTime.Today;
while (startingDate.DayOfWeek != weekStart)
startingDate = startingDate.AddDays(-1);
DateTime previousWeekStart = startingDate.AddDays(-7);
Is this the best way in c#
Thanks
As previously stated the duplicate's response will probably do for you
mondayOfLastWeek = DateTime.Now.AddDays( -(int)DateTime.Now.DayOfWeek - 6 );
sundayOfLastWeek = DateTime.Now.AddDays(-(int)DateTime.Now.DayOfWeek)
Try:
DateTime.Today.AddDays(-7);
This question already has answers here:
Calculate difference between two dates (number of days)?
(17 answers)
Closed 9 years ago.
We want to find the number of days between two dates. This is simple when the dates are in the same year.
Is there a built-in way to do this if the dates are in different years, or do we just have to loop through each year?
Subtracting a date from another yields a TimeSpan. You can use this to determine the number of whole days using the Days property, or whole and fractional days using the TotalDays property.
DateTime start = ...;
DateTime end = ...;
int wholeDays = (end - start).Days;
or
double totalAndPartialDays = (end - start).TotalDays;
you can probably do something like:
TimeSpan ts = endDate - startDate;
ts.Days
What are you missing?
DateTime - DateTime => Timespan
and Timespan has Days and TotalDays properties.
DateTime date1 = DateTime.Now;
DateTime date2 = new DateTime(date1.Year - 2, date1.Month, date1.Day);
Int32 difference = date1.Subtract(date2).Days;