How can I reduce a specific duration from a date?
for example -
the base date and time to reduce from
15/05/2018 02:00:00
the duration to reduce -
03:00:00 hours
the expected output -
14/05/2018 23:00:00
=A1-3/24-5/(24*60)-55/(24*60*60)
where
A1 = 15/05/2018 02:00:00
3 = the number of hours to reduce
5 = minutes
55 = seconds
If you have time in format: 03:05:55, the formula is simple:
=A1 - A2
where
A1 = 15/05/2018 02:00:00
A2 = 03:05:55
Related
How can I find the average of 3 date in Ruby on rails or Ruby ? like bellow.
(Date1 + Date2 + Date3)/3
If you convert the dates to integers with .to_i you can do the average exactly as you suggested. Then use .at to get back to a datetime.
d1 = Time.now
=> 2022-03-16 11:07:12 -0700
d2 = Time.now - 10000
=> 2022-03-16 08:20:32 -0700
d3 = Time.now - 30000
=> 2022-03-16 02:47:12 -0700
Time.at((d1.to_i + d2.to_i + d3.to_i)/3)
=> 2022-03-16 07:24:58 -0700
first_date = Date.today.strftime("%Y%m%d").to_i
second_date = Date.tomorrow.strftime("%Y%m%d").to_i
third_date = Date.yesterday.strftime("%Y%m%d").to_i
average_date = (first_date + second_date + third_date) / 3
Date.parse(average_date.to_s) OR Time.at(average_date)
If you are working with Date objects (Not Time nor DateTime) you can easily calculate the middle date between 3 (or even more dates if you add them to the array).
Imagine you have your dates in an array:
dates = [Date.today, 1.day.ago, 10.days.ago]
difference = dates.max - dates.min
average_date = dates.min + difference/2
Basically, we're getting the days difference between the higher date value and the minimum date value, then adding half that difference to the smallest date, and there you have the average date between the 3, the date in the middle won't change anything so it's not being used with this approach.
How can I find the average of 3 date in Ruby on rails or Ruby ? like bellow.
(Date1 + Date2 + Date3)/3
If you convert the dates to integers with .to_i you can do the average exactly as you suggested. Then use .at to get back to a datetime.
d1 = Time.now
=> 2022-03-16 11:07:12 -0700
d2 = Time.now - 10000
=> 2022-03-16 08:20:32 -0700
d3 = Time.now - 30000
=> 2022-03-16 02:47:12 -0700
Time.at((d1.to_i + d2.to_i + d3.to_i)/3)
=> 2022-03-16 07:24:58 -0700
first_date = Date.today.strftime("%Y%m%d").to_i
second_date = Date.tomorrow.strftime("%Y%m%d").to_i
third_date = Date.yesterday.strftime("%Y%m%d").to_i
average_date = (first_date + second_date + third_date) / 3
Date.parse(average_date.to_s) OR Time.at(average_date)
If you are working with Date objects (Not Time nor DateTime) you can easily calculate the middle date between 3 (or even more dates if you add them to the array).
Imagine you have your dates in an array:
dates = [Date.today, 1.day.ago, 10.days.ago]
difference = dates.max - dates.min
average_date = dates.min + difference/2
Basically, we're getting the days difference between the higher date value and the minimum date value, then adding half that difference to the smallest date, and there you have the average date between the 3, the date in the middle won't change anything so it's not being used with this approach.
I need the number of hours, minutes, seconds between two dates and times.I'm able to get the number of days, hours, minutes, seconds but I don't want no.of days instead of it, I need hours, minutes, seconds only enough.
Here my code,
start_time is Wed, 13 Dec 2017 20:35:19 -0800 and end_time is today datetime
def time_diff(end_time, start_time)
diff = end_time - start_time
mm, ss = diff.divmod(60)
hh, mm = mm.divmod(60)
dd, hh = hh.divmod(24)
time = "%d h, %d m, %d s" % [hh, mm, ss]
return time
end
I need output like this "35 h, 29 m, 12 s"
Thanks for your help.
Just out of curiosity, a pure [almost] functional solution, without intermediate local variables:
start_time = DateTime.parse 'Wed, 13 Dec 2017 23:00:00 UTC'
end_time = DateTime.parse 'Wed, 15 Dec 2017 23:30:20 UTC'
sec, min, hrs = [60, 60, 1].
map.
with_object([[[end_time, start_time].
map(&:to_time).
map(&:to_i).
reduce(:-), nil]]) do |div, obj|
obj << obj.last.first.divmod(div)
obj[-2].rotate!
end.
map(&:first).
compact
#⇒ [20, 30, 48]
You've already got the answer - just don't divide by 24!
If the start_time and end_time are DateTime value you can use the following
difference = end_time - start_time
hours = (difference * 24).to_i
minutes = (difference * 24 * 60).to_i
seconds = (difference * 24 * 60 * 60).to_i
let date = NSDate()
let future = NSDate.distantFuture()
let f = NSDateComponentsFormatter()
f.unitsStyle = .Full
f.allowedUnits = [.Year, .Month, .Day]
f.stringFromTimeInterval(future.timeIntervalSinceDate(date))
Result is "-57 years, 0 months, 26 days" which is wrong.
I thought this might cause from overflow, so I try smaller number and found that this weird behavior start at 69 years interval
let date = NSDate()
let sixtyEightYears = NSCalendar.currentCalendar().dateByAddingUnit(.Year, value: 68, toDate: date, options: NSCalendarOptions())!
let sixtyNineYears = NSCalendar.currentCalendar().dateByAddingUnit(.Year, value: 69, toDate: date, options: NSCalendarOptions())!
let f = NSDateComponentsFormatter()
f.unitsStyle = .Full
f.allowedUnits = [.Year, .Month, .Day]
future.timeIntervalSinceDate(date)
sixtyEightYears.timeIntervalSinceDate(date) // 2145916800
sixtyNineYears.timeIntervalSinceDate(date) // 2177452800
f.stringFromTimeInterval(sixtyNineYears.timeIntervalSinceDate(date)) // "-67 years, 1 month, 6 days"
Is this the Apple bug, or I did something wrong?
The method timeIntervalSinceDate whose reference date is 1 January 1970 causes the problem.
1970 + 68 = 2038
From Wikipedia:
Year 2038 problem
The Year 2038 problem is an issue for computing and data storage
situations in which time values are stored or calculated as a signed
32-bit integer, and this number is interpreted as the number of
seconds since 00:00:00 UTC on 1 January 1970 ("the epoch"). Such
implementations cannot encode times after 03:14:07 UTC on 19 January
2038, a problem similar to but not entirely analogous to the "Y2K
problem" (also known as the "Millennium Bug"), in which 2-digit values
representing the number of years since 1900 could not encode the year
2000 or later. Most 32-bit Unix-like systems store and manipulate time
in this "Unix time" format, so the year 2038 problem is sometimes
referred to as the "Unix Millennium Bug" by association.
Full article : Year 2038 problem
I have a web service which returns date of birth of people. The issue that I am having is when the year of birth is around 1899 the time looses few hours. However, the more close it gets to 1970 it gets accurate. There are no issues when the birth year is after 1970.
Passed date - 1899-12-31 16:00:00 +0000
Returning date - 1899-12-31 22:55:25 +0000 (added time difference of +0800)
after 1970 is fine]
Passed date - 1990-08-24 16:00:00 +0000
Returning date - 1990-08-25 00:00:00 +0000
Im using objective c
jsonDateString is the passing string
double dateNumber = [[jsonDateString substringWithRange:NSMakeRange(startPosition, 1)] isEqualToString:#"-"] ? - [[jsonDateString substringWithRange:NSMakeRange(startPosition + 1, 13)] doubleValue] : [[jsonDateString substringWithRange:NSMakeRange(startPosition, 13)] doubleValue];
NSTimeInterval unixTime = dateNumber / 1000; //WCF will send 13 digit-long value for the time interval since 1970 (millisecond precision) whereas iOS works with 10 digit-long values (second precision), hence the divide by 1000
// time changes***** - by ashan
NSDate *gmtDate = [[NSDate dateWithTimeIntervalSince1970:unixTime]dateByAddingTimeInterval:0];
NSTimeInterval timeZoneOffset = [[NSTimeZone systemTimeZone] secondsFromGMTForDate:gmtDate];
NSDate *localDate = [gmtDate dateByAddingTimeInterval:timeZoneOffset];
JsonString - /Date(-2209017600000+0800)/
dividedby1000 : -2209017600.000000
Passed date - 1899-12-31 16:00:00 +0000
Returning date - 1899-12-31 22:55:25 +0000
As you can see even you convert time stamp and it says 1600 and +8 would 0000. How ever it shows 1100h. Is it a issue with the epoch unix timing before 1970 or db issue on the server side.