Android Question Date arithmetic: subtract days from current date

TyneBridges

Member
Licensed User
Longtime User
I'm trying to do some simple date arithmetic in B4A but the logic of this defeats me, even with DateUtils loaded. All I want to do is subtract a given number of days from the current date and return the new date as a string.

I know I can get the current date using DateTime.Now and extract parts of it with DateTime.GetDayOfMonth(OurDate). I can see that Periods and SetDateAndTime might be able to do what I want but, without an example, I can't figure out the code for this.

Evidently I can't use a statement like OurStartDate = DateTime.Subtract(DateTime.Now, 375) because DateTime.Subtract is in Java but not B4A...?

I've searched on line because I thought the answer to this would be on line, but I can't find it. Maybe it's so simple that no-one else has asked, and I'm just dim. Is there a simple way to do this?

Thanks.
 

TyneBridges

Member
Licensed User
Longtime User
Thanks imbault - that's great. I had searched the documentation and missed that, although "Subtract" isn't valid, "Add" is.

A supplementary query - although my date variables are declared as strings and I precede the assignation statement with DateTime.DateFormat = "yyyy-MM-dd", the dates aren't returned in the expected format, but as long numbers. I've obviously missed how to tell B4A that the string contents are dates...
 
Upvote 0

TyneBridges

Member
Licensed User
Longtime User
Sorry, got it - the syntax is convoluted but it's (e.g.) StartDt = DateTime.Date(DateTime.Add(DateTime.Now, 0, 0, -375))
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…