.net - Function to get a custom Date Difference -
i'm trying code generic function return difference between 2 dates in customized format, 1 person reported me snippet has error example trying difference between 2 dates:
15/08/2013 - 02/09/2013
and function returns negative values , 1 month of difference 'cause 09 greater 08, it's not working need:
1 months, -1 weeks, -6 days, 0 hours, 0 minutes , 0 seconds
the expected result this:
0 months, 2 weeks, 4 days, 0 hours, 0 minutes , 0 seconds
someone can me correct error in dates?
here code:
#region " date difference " ' date difference ' ' // elektro h@cker ' ' examples : ' ' msgbox(datedifference(datetime.parse("01/03/2013"), datetime.parse("10/04/2013"))) ' result: 1 months, 1 weeks, 2 days, 0 hours, 0 minutes , 0 seconds ' msgbox(datedifference(datetime.parse("01/01/2013 14:00:00"), datetime.parse("02/01/2013 15:00:30"))) ' result: 0 months, 0 weeks, 1 days, 1 hours, 0 minutes , 30 seconds private function datedifference(byval date1 datetime, byval date2 datetime) string dim monthdiff string, weekdiff string, _ daydiff string, hourdiff string, _ minutediff string, seconddiff string monthdiff = convert.tostring(datediff("m", date1, date2)) weekdiff = convert.tostring(datediff("d", date1.addmonths(datediff("m", date1, date2)), date2) \ 7) daydiff = convert.tostring(datediff("d", date1.addmonths(datediff("m", date1, date2)), date2) - (weekdiff * 7)) hourdiff = convert.tostring(datediff("h", date1.addhours(datediff("h", date1, date2)), date2) - (date1.hour - date2.hour)) minutediff = convert.tostring(datediff("n", date1.addminutes(datediff("n", date1, date2)), date2) - (date1.minute - date2.minute)) seconddiff = convert.tostring(datediff("s", date1.addseconds(datediff("s", date1, date2)), date2) - (date1.second - date2.second)) return string.format("{0} months, {1} weeks, {2} days, {3} hours, {4} minutes , {5} seconds", _ monthdiff, weekdiff, daydiff, hourdiff, minutediff, seconddiff) end function #end region
update:
i'm trying way, how substract weeks ?
private function datedifference(byval date1 datetime, byval date2 datetime) string
dim monthdiff string, weekdiff string, _ daydiff string, hourdiff string, _ minutediff string, seconddiff string monthdiff = date2.month - date1.month ' weekdiff = date2.month - date1.month daydiff = date2.day - date1.day hourdiff = date2.hour - date1.hour minutediff = date2.minute - date1.minute seconddiff = date2.second - date1.second ' msgbox((date2 - date1).tostring("dd")) return string.format("{0} months, {1} weeks, {2} days, {3} hours, {4} minutes , {5} seconds", _ monthdiff, weekdiff, daydiff, hourdiff, minutediff, seconddiff)
end function
update 2:
i'm trying rewrite using other method date accuracy i¡m totally lost:
private function datedifference(byval date1 datetime, byval date2 datetime) string dim daydiff long = date2.subtract(date1).days dim hourdiff long = date2.subtract(date1).hours dim minutediff long = date2.subtract(date1).minutes dim seconddiff long = date2.subtract(date1).seconds dim millidiff long = date2.subtract(date1).milliseconds dim monthdiff long dim weekdiff long select case (daydiff mod datetime.daysinmonth(date1.year, date1.month)) case <= 0 monthdiff = 0 case = 1, <= 28 monthdiff = 1 case > 28 end select msgbox(daydiff mod datetime.daysinmonth(date1.year, date1.month)) msgbox(monthdiff) ' return string.format("{0} months, {1} weeks, {2} days, {3} hours, {4} minutes , {5} seconds", _ ' monthdiff, weekdiff, t.days, t.hours, t.minutes, t.seconds) end function
update 3:
i'm more near code i've said i'm lost calculating things:
private function datedifference(byval date1 datetime, byval date2 datetime) string dim daydiff long = date2.subtract(date1).days dim hourdiff long = date2.subtract(date1).hours dim minutediff long = date2.subtract(date1).minutes dim seconddiff long = date2.subtract(date1).seconds dim millidiff long = date2.subtract(date1).milliseconds dim monthdiff long dim weekdiff long x short = cshort(date1.month) cshort(date2.month) monthdiff = msgbox(daydiff - datetime.daysinmonth(date1.year, x)) 'msgbox(datetime.daysinmonth(date1.year, x)) next ' msgbox(daydiff - datetime.daysinmonth(date1.year, date1.month)) ' msgbox(monthdiff) ' return string.format("{0} months, {1} weeks, {2} days, {3} hours, {4} minutes , {5} seconds", _ ' monthdiff, weekdiff, t.days, t.hours, t.minutes, t.seconds) end function
this calculates months , weeks programmatically, uses timespan
remaining portions. month calculation, tricky, uses intelligent behavior of addmonths
.
private function datedifference(byval date1 datetime, byval date2 datetime) string dim monthdiff = 0 while date1 <= date2 date1 = date1.addmonths(1) monthdiff += 1 end while monthdiff -= 1 date1 = date1.addmonths(-1) dim t timespan = date2 - date1 dim weekdiff integer = t.days \ 7 t = t - timespan.fromdays(weekdiff * 7) return string.format("{0} months, {1} weeks, {2} days, {3} hours, {4} minutes , {5} seconds", _ monthdiff, weekdiff, t.days, t.hours, t.minutes, t.seconds) end function
Comments
Post a Comment