This question already has an answer here:
Dividing HH:MM:SS by an integer
(1 answer)
Closed 5 years ago.
Is there a easy solution for dividing a time? For example divide 04:00:30 by two and get 2:00:15?
select FIRMA, NR, CAST(Zeit AS TIME(0)) as Zeit
from [2_2017]
where Art='D'
and Zeit is not Null
and Zeit !='00:00:00'and Tour_1 ='0'
order by Firma,NR
Perhaps you first need to convert time to some integer as sec, min , hours from midnight and then devide it and add back to midnight
DECLARE #zeint time = '23:00:15'
declare #midnight time ='00:00:00'
select
dateadd(SECOND,
(DATEDIFF(SECOND,#midnight, #zeint)/2)
,#midnight) as half
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want to find difference between two DateTime objects. I know I can use DateTime.Subtract() and overloaded operator '-' (e.g DateTime - DateTime). But someone told me that these two techniques are not good enough, and there is a method DateDiff or something, which I should use and which is best to use. I want to know if it is true? If it is, then how the above two are inferior from any perspective?
DateDiff does not compare dateTime instances.
It compares specific parts of the DateTime instances - for example, the months, or hours.
For instance:
var lasyDayOf2018 = new DateDime(2018, 12, 31);
var firstDayOf2019 = lasyDayOf2018.AddDays(1);
var monthDiff = DateDiff(DateInterval.Month, lasyDayOf2018, firstDayOf2019)
monthDiff will be 1, even though only one day has passed between the two dates.
It's documented in the Remarks section of the DateDiff documentation page:
If Interval is set to DateInterval.Year, the return value is calculated purely from the year parts of Date1 and Date2. Similarly, the return value for DateInterval.Month is calculated purely from the year and month parts of the arguments, and for DateInterval.Quarter from the quarters containing the two dates.
For example, when comparing December 31 to January 1 of the following year, DateDiff returns 1 for DateInterval.Year, DateInterval.Quarter, or DateInterval.Month, even though at most only one day has elapsed.
To get the actual difference between different instances of DateTime, either use Subtract or -, as you wrote in the question.
Whether or not there is a problem, and the solution to that problem really depends on the situation and why you're computing the difference. There are two main problems with DateTime.Subtract() and operator -.
The first is that they both ignore the time zone portion of the data. So, for example, if you have two dates, 2019-07-18 12:00:00 UTC and 2019-07-18 06:00:00 MDT (local time), even though they represent the same moment in time (as measured from different time zones), DateTime.Subtract() will tell you that they're 6 hours apart.
The second problem is that you often want to know the answer in terms of units other than ticks, sometimes even something that cannot be converted to a fixed number of ticks. For example, if you want the difference in the months represented by two DateTimes, you cannot compute that from the difference in ticks (which is what DateTime.Subtract() is based on), because different months have different lengths. For example, for the dates '2019-06-30 23:59:59' and '2019-07-01 00:00:00' (in the same time zone) there is only a one second difference, but the months are different, so is the difference in months one or zero? It depends on what you want to use the answer for.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Input value is only like this in string value and nothing else: P0DT3H40M0S
This indicates travel time and needs to be converted to hours and minutes.
D signifies Day, T is Time (right part signifies Time, above sample was: 3 Hours And 40 Minutes and 0 Seconds. H-hours, M-Minutes, S-Seconds)
If there's a value in D, eg. P1DT3H40M0S - this already signifies 1 Day and 3 Hours and 40 minutes and 0 Seconds, so output should be 27 Hours and 40 minutes. How do i get the values for Days, Hours and minutes using Regex? or can i use substring? my problem is if there were 2 digit Days and Hours, substring won't work. i need to get the numbers leftside the D, H and M.
You can use TimeSpan.ParseExact to parse this string into a TimeSpan
var pattern=#"\Pd\D\Th\Hm\Ms\S";
var ts = TimeSpan.ParseExact("P01DT3H40M0S",pattern,CultureInfo.InvariantCulture);
You can use the TimeSpan object in calculations. It supports addition, subtraction and comparison.
You can extract the total duration in hours using TotalHours. In this case, this will return 27.6666666. You can get the whole hours just by casting to int, (int)ts.TotalHours will return 27.
Copying from one of Jon Skeet's answers you can format the string as hours/minutes by using the truncated TotalHours and the Minutes, Seconds component:
String.Format("{0}:{1}:{2}",(int)ts.TotalHours,ts.Minutes,ts.Seconds)
This will return 27:40:0
This question already has answers here:
Calculate difference between two dates (number of days)?
(17 answers)
Closed 6 years ago.
My sysadmin has failed to actually expire passwords in AD and now my password expiry warning system is starting to count back up instead of showing negative days. I'm expecting remainingDays to be a negative number for my test account ( 5 days pseudo-expired) from the following code, hoping someone can show me why I'm losing the negative. From what I've read on MSDN, DateTime.Subtract can return negative values.
DateTime today = DateTime.Now;
foreach (User user in users)
{
DateTime expiryDate = user.pwdLastReset.AddDays(180); //pwd expires every 180 days
int remainingDays = Int32.Parse(expiryDate.Subtract(today).ToString("%d"));
//snipped code to send warnings at different days remaining.
}
DateTime.ToString("%d") will actually return the day of the month represented. For example, the following code:
DateTime prev = DateTime.Now.Subtract(TimeSpan.FromDays(10));
Console.WriteLine(prev.ToString("%d"));
prints out "15" (as in, "July 15, 2016"), not 10 or -10.
This question already has answers here:
Add hours or minutes to the current time
(4 answers)
Closed 7 years ago.
I get the current time and need to add 1 minute to it so It would be 1 minute ahead, how do I do that?
string date1 = System.DateTime.Now.ToString("HH:mm");
All you have to use is DateTime.AddMinutes
string date1 = System.DateTime.Now.AddMinutes(1).ToString("HH:mm");
Conversely, if you want to subtract a minute just use a negative parameter.
DateTime.AddMinutes method can do that.
If you want a string representation then do:
string date1 = System.DateTime.Now.AddMinutes(1).ToString("HH:mm");
As a side note, you can also supply a negative number to subtract minutes from current time like:
DateTime.Now.AddMinutes(-1)
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I calculate someone’s age
How can i calculate age using datetimepicker in c#?
Strictly speaking,
TimeSpan age = DateTime.Now - dateTimePicker.Value;
However, figuring out someone's "age" is only slightly more complicated.
int years = DateTime.Now.Year - dateTimePicker.Value.Year;
if(dateTimePicker.Value.AddYears(years) > DateTime.Now) years--;
Because years vary in length you'll have to do this rather than relying on a structure like the TimeSpan that represents a specific amount of time (the same is true for figuring out how many "months" are between two dates, since months vary in length from 28-31 days).
The last line of code is there to account for the person's birthday not yet taking place this year.
Assuming that the DateTimePicker is called dtpBirthday:
int age = DateTime.Now.Year - dtpBirthday.Value.Year - (DateTime.Now.DayOfYear < dtpBirthday.Value.DayOfYear ? 1 : 0);