This question already has answers here:
Adding a TimeSpan to a given DateTime
(8 answers)
Closed 3 years ago.
A bit silly question, but I find myself not sure how to answer it.
Timespan ts = (DateTime1 - DateTime2).TotalMinutes
Suppose I know ts and DateTime2, how can I find DateTime1?
You can add a TimeSpan to a date
TimeSpan ts = DateTime1 - DateTime2;
DateTime1 = DateTime2 + ts;
Note that I removed the TotalMinutes, because it returns a double, not a TimeSpan.
If you want to work with minutes, you can write
double minutes = (DateTime1 - DateTime2).TotalMinutes;
DateTime1 = DateTime2 + TimeSpan.FromMinutes(minutes);
Note that the minutes contain the seconds and fractions of seconds as decimals. If you only need the full minutes, you can get them with:
int fullMinutes = (int)Math.Floor(minutes);
Your code does not compile.
Example:
DateTime DateTime1 = DateTime.Now;
DateTime DateTime2 = DateTime1.AddMinutes(-10);
var ts = (DateTime1 - DateTime2).TotalMinutes; // ts is a double
DateTime DateTime3 = DateTime1 + TimeSpan.FromMinutes(ts);
Related
This question already has answers here:
DateTime.Value.ToString(format) gives me 12 hour clock
(4 answers)
Closed 6 years ago.
I'm working with a project and one of its functional requirement is to create a datetime range in which I will select a set of data based on that.
This range should take today's datetime starting from 8:00:00 to 18:00:00, and then I want to convert the format to be yyyy-MM-ddThh:mm:ssZ, so I'm doing the following to approach that:
DateTime fromTime = DateTime.Now;
TimeSpan ts = new TimeSpan(8,0,0);
fromTime = fromTime.Date + ts;
string fromTimeFormat = fromTime.ToString("yyyy-MM-ddThh:mm:ssZ");
DateTime toTime = DateTime.Now;
TimeSpan tss = new TimeSpan(18, 0, 0);
toTime = toTime.Date + tss;
string toTimeFormat = toTime.ToString("yyyy-MM-ddThh:mm:ssZ");
The problem is that, the toTimeFormat is being converted to the 12h system, so when I use it later it's being considered as 6:00 AM.
Any ideas please?
Because you are using hh specifier which is for 12-hour clock format.
You need to use HH specifier which is for 24-hour clock format.
string toTimeFormat = toTime.ToString("yyyy-MM-ddTHH:mm:ssZ");
And you can simplify your code as;
string fromTimeFormat = DateTime.Today.AddHours(6).ToString("yyyy-MM-ddThh:mm:ssZ");
string toTimeFormat = DateTime.Today.AddHours(18).ToString("yyyy-MM-ddTHH:mm:ssZ");
I must have built up such that I have a datetime which gets added antale day as it should go forward. and then I have time as it should set off in relation to 04/10/16 to 10/09/16
I do not care about the time which is in datetime. It should not I use for anything. What I need out of this is exactly how many days there are from that time.
Datetime dateString = "4/10/2016 8:30:52" //I pretend that it comes from the database, it was more in terms of see what come there.
DateTime dt = DateTime.Now.AddDays(5);
What I need out of this is that it tell me how many days there are in between the two datetime as I entered.
You can substract DateTime objects to obtain a TimeSpan:
Datetime dateString = DateTime.Parse("4/10/2016 8:30:52");
DateTime dt = DateTime.Now;
TimeSpan duration = dt-dateString;
From the TimeSpan object, you can get how many (full) days with :
int totalCompleteDays = (int)duration.TotalDays;
Or if you want a rounded results :
int roundedTotalDays = (int)Math.Round(duration.TotalDays);
DateTime objects support basic operators and will return TimeSpan objects.
DateTime DateTimeB = DateTime.Now.AddDays(5);
DateTime DateTimeA = DateTime.Now;
TimeSpan difference = DateTimeA - DateTimeB;
...
you can then use the TotalDays property of the timeSpan.
...
Console.out.WriteLine(difference.TotalDays);
This question already has answers here:
How do I get the time difference between two DateTime objects using C#?
(9 answers)
Closed 7 years ago.
I have two dates of the form:
Start Date: 2007-03-24
End Date: 2009-06-26
Now I need to find the difference between these two in the following form:
2 years, 3 months and 2 days
How can I do this in c# windows form?
You need to use TimeSpan to get difference..
class Program
{
static void Main(string[] args)
{
string StartDate = "2007-03-24";
string EndDate = "2009-06-26";
System.DateTime firstDate = DateTime.ParseExact(StartDate, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
System.DateTime secondDate = DateTime.ParseExact(EndDate, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
System.TimeSpan diff = secondDate.Subtract(firstDate);
var totalDays = (diff).TotalDays;
var totalYears = Math.Truncate(totalDays / 365);
var totalMonths = Math.Truncate((totalDays % 365) / 30);
var remainingDays = Math.Truncate((totalDays % 365) % 30);
Console.WriteLine("Estimated duration is {0} year(s), {1} month(s) and {2} day(s)", totalYears, totalMonths, remainingDays);
Console.ReadLine();
}
}
.net already provide TimeSpan class to show differences between two datetime value.some properties of TimeSpan class show years,month and day interval seperately
I am trying to find days between two dates using a Time span.
nextdate1='2014-12-20'
today `='2014-12-18'`
My sample code is:
DateTime nexdate1 = dr.GetDateTime(2); //gets from database. I checked and the value is correct
DateTime today = DateTime.Now;
TimeSpan nextdate = nexdate1.Subtract(today);
int difference = nextdate.Days;
Now I get difference=1. Actually the difference is 2 (20-18).
Why it shows difference as 1?
TimeSpan.Days is an int. Your answer is getting truncated.
In the following code:
var date1 = DateTime.Parse("Dec 12, 2014");
var date2 = DateTime.Parse("Dec 14, 2014");
var difference = (date2 - date1).Days;
difference is set to 2.
But in this code:
var date1 = DateTime.Parse("Dec 12, 2014 12:01 AM");
var date2 = DateTime.Parse("Dec 14, 2014");
var difference = (date2 - date1).Days;
difference is set to 1.
When we look at the timespan date2 - date1, we get the following:
{1.23:59:00}
Days: 1
Hours: 23
Minutes: 59
Seconds: 0
TotalDays: 1.9993055555555554
You should set nextDate = nexdate1.Date.Subtract(DateTime.Today); so that you're only looking at the difference in days, or take (int)Math.Round(nextDate.TotalDays)
Actually your datetime variable having time part, because of that time your result get affected. Hence you have to find only date part of your datetime value:
Use DateTime.Date
Do it like this:
DateTime nexdate1 = dr.GetDateTime(2);
DateTime today = DateTime.Now;
TimeSpan nextdate = nexdate1.Date.Subtract(today.Date); // Here find only date part from datetime value.
//TimeSpan nextdate = nexdate1-today
int difference = nextdate.Days;
This question already has answers here:
How to change time in DateTime?
(29 answers)
Closed 9 years ago.
I have a DateTime variable:
DateTime date = DateTime.Now;
I want to change the time part of a DateTime variable. But when I tried to access time part (hh:mm:ss) these fields are readonly.
Can't I set these properties?
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);
you can't change the DateTime object, it's immutable. However, you can set it to a new value, for example:
var newDate = oldDate.Date + new TimeSpan(11, 30, 55);
date = new DateTime(date.year, date.month, date.day, HH, MM, SS);
I'm not sure exactly what you're trying to do but
you can set the date/time to exactly what you want in a number of ways...
You can specify 12/25/2010 4:58 PM by using
DateTime myDate = Convert.ToDateTime("2010-12-25 16:58:00");
OR if you have an existing datetime construct , say 12/25/2010 (and any random time) and you want to set it to 12/25/2010 4:58 PM, you could do so like this:
DateTime myDate = ExistingTime.Date.AddHours(16).AddMinutes(58);
The ExistingTime.Date will be 12/25 at midnight, and you just add hours and minutes to get it to the time you want.
It isn't possible as DateTime is immutable. The same discussion is available here: How to change time in datetime?