Hi.
I need to convert year, month and date in a date in the XOJO iOS Framework.
Under XOJO Desktop Framework it is as simple as:
dim d as date
d = new date
d.year = 2015
d.month = 7
d.day = 1
but this doesnt work in the XOJO iOS Framework.
Anyone could give me a help. Thanks and sorry because of so basic problem I ask for help.
See http://developer.xojo.com/xojo-core-date. The new date type is immutable so you can’t change the properties like that.
You need to use the constructor which specifies all of the year/month/day/etc values.
[code] Dim d As New xojo.Core.Date(2015, 7, 1, 0, 0, 0, New Xojo.Core.TimeZone(“NZ”))
[/code]
You’ll want to adjust the timezone to suit.
Thanks a lot Jason and Wayne !!!
I used Wayne´s code and works fine.
Does someone know a more eficient and accurate way to calculate the difference in days between two dates ?
I come from VB where the function DateDiff(“d”, d1, d2) solves the problem
This works fine, but I beleive that must be something more profesional:
Dim diff_dates_in_days as Integer
Dim d1 As New xojo.Core.Date(2015, 7, 1, 0, 0, 0, New Xojo.Core.TimeZone(“NZ”))
Dim diff_days, diff_months, diff_years as Integer
diff_days = Date.Now.day - d1.day
diff_months = Date.Now.month - d1.month
diff_years = Date.Now.year - d1.year
diff_dates_in_days = diff_years * 365 + diff-months * 30 + diff_days
Dateinterval will give you the difference between two dates, but you’d still need to work out the days.
Dim diff As xojo.Core.DateInterval = xojo.Core.Date.now - d
[quote=205613:@Wayne Golding]Dateinterval will give you the difference between two dates, but you’d still need to work out the days.
Dim diff As xojo.Core.DateInterval = xojo.Core.Date.now - d
[/quote]
You should be able to get days from Diff.Days in that case.
Diff.days will give you the number of days e.g. 8/10/15 - 7/1/15 = 9 days which is not what you’d get using VB (40 days total).
Thanks so much all.
You are correct. Another way to do this is:
dim d1 as Double = Date1.SecondsFrom1970
Dim d2 as Double = Date2.SecondsFrom1970
Dim diffdays as double = Xojo.Math.Abs(d1 - d2) / 86400