Android Question Converting a string

Sergey_New

Well-Known Member
Licensed User
Longtime User
Please tell me how to convert the string "21/01/2020" to "01/21/2020".
I need this to get ticks from a date in "MM.dd.yyyy" format using DateTime.DateParse
 
Last edited:

emexes

Expert
Licensed User
Longtime User
Upvote 0

emexes

Expert
Licensed User
Longtime User
Both answers above are correct

Hence why I quoted @William Lancee 🍻


On a related note: we had to handle this for a program that accepted user input in the USA (ie month first) and almost everywhere else (day first) except for Japan (year first) and we solved it by having a setting that specified the order of the date components, and another setting that enabled automatic updating of the order setting according to actual input dates ie if a component was > 31 then it couldn't be a month or day value, and if it was > 12 then it couldn't be a month value.

Worked great, unless you had a workshop with people who thought in different date styles.

Also handled dates like 20/6/7 (and 200607 and 607 and 0720).

On the output side, we defaulted to using Jan..Dec for the month component, but then we needed to cope with non-english variations.

Internally all dates were stored as an 8 digit (32-bit) number yyyymmdd. Zero problems with y2k rollover. Lol heck just realised we're already a quarter of the way to y21c rollover.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Hence why I quoted @William Lancee 🍻


On a related note: we had to handle this for a program that accepted user input in the USA (ie month first) and almost everywhere else (day first) except for Japan (year first) and we solved it by having a setting that specified the order of the date components, and another setting that enabled automatic updating of the order setting according to actual input dates ie if a component was > 31 then it couldn't be a month or day value, and if it was > 12 then it couldn't be a month value.

Worked great, unless you had a workshop with people who thought in different date styles.

Also handled dates like 20/6/7 (and 200607 and 607 and 0720).

On the output side, we defaulted to using Jan..Dec for the month component, but then we needed to cope with non-english variations.

Internally all dates were stored as an 8 digit (32-bit) number yyyymmdd. Zero problems with y2k rollover. Lol heck just realised we're already a quarter of the way to y21c rollover.
Managing dates (and times) is one of the most complicated and tedious tasks in programming, especially when you have to store them in different DBMS. It's a headache!
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
It's a headache!
Yes, that's true.
My database stores dates as "27 MAY 2025" and only the year, for example "2020". Dates can also have prefixes "before", "after" and the like.
The program is supposed to display different date formats:
dd.MM.yyyy, dd/MM/yyyy, dd-MM-yyyy, MM.dd.yyyyand several other formats and calculate the period between dates. I managed the first three formats, but the last one is a bummer.
If anyone can help, please let me know.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Yes, that's true.
My database stores dates as "27 MAY 2025" and only the year, for example "2020". Dates can also have prefixes "before", "after" and the like.
The program is supposed to display different date formats:
dd.MM.yyyy, dd/MM/yyyy, dd-MM-yyyy, MM.dd.yyyyand several other formats and calculate the period between dates. I managed the first three formats, but the last one is a bummer.
If anyone can help, please let me know.
I'm afraid it's virtually impossible to help you without having your project and your database (and we don't even know if it's SQLite, but I assume so).

Perhaps the simplest thing would be to save the dates in the database in text format, ISO 8601 (YYYY-MM-DD), and perform all the necessary operations using B4X code.
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
Perhaps the simplest thing would be to save the dates in the database in text format, ISO 8601 (YYYY-MM-DD), and perform all the necessary operations using B4X code.
The program must save dates in the original format when changing data. This means that you will have to do the reverse conversion of dates.
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
but do you always know the original date format?
Yes, I know. Prefixes can be ABT, AFT, BEF, BET, CAL, EST, FROM,TO, AND.
Date can be 21.01.2020 or 2000 and the required prefix or without it.
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
do you know all the date formats you need to convert?
In the application I need to give users a choice of data formats they are accustomed to. These formats are:
"dd.MM.yyyy", "dd/MM/yyyy", "dd-MM-yyyy", "MM.dd.yyyy", "MM/dd/yyyy", "MM-dd-yyyy", "yyyy/MM/dd", "yyyy-MM-dd".
How do you get those dates, and what format are they in?
Date can be 21.01.2020 or 2000 and the required prefix or without it.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
In the application I need to give users a choice of data formats they are accustomed to. These formats are:
"dd.MM.yyyy", "dd/MM/yyyy", "dd-MM-yyyy", "MM.dd.yyyy", "MM/dd/yyyy", "MM-dd-yyyy", "yyyy/MM/dd", "yyyy-MM-dd".
See #2

All of them are parseable if you set the Dateformat correctly.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
In the application I need to give users a choice of data formats they are accustomed to. These formats are:
"dd.MM.yyyy", "dd/MM/yyyy", "dd-MM-yyyy", "MM.dd.yyyy", "MM/dd/yyyy", "MM-dd-yyyy", "yyyy/MM/dd", "yyyy-MM-dd".
Try:
B4X:
Public Sub DateStrToTicks(UserDateFrmt As String, strData As String) As Long
    DateTime.DateFormat = UserDateFrmt
    Return DateTime.DateParse(strData)
End Sub

Public Sub DateTicksToString(UserDateFrmt As String, lngData As Long) As String
    DateTime.DateFormat = UserDateFrmt
    Return DateTime.Date(lngData)
End Sub
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
All of them are parseable if you set the Dateformat correctly.
Agreed.
I already said that I have solved everything for the first three formats. For subsequent formats, how to convert dates is also clear. The problem with them is how to convert data taking into account prefixes.
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
About, After, Before, ...?
Correct.
But explain better what the problem is, or rather how it works in general.
This is what might be in the database:
"AFT 30 MAY 2001"
From this data, you need to get the first date and its prefixes
and then get the ticks from the date.
Or more complex data:
"BET 30 MAY 2001 AND 2002"
From this, you need to get both dates, prefixes, and ticks.
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
Found out why my program doesn't work with other data formats.
About 10 years ago I wrote a pattern that converts dates to the required format.
B4X:
Dim patDate As String="((?:(?:ABT|AFT|BEF|BET|CAL|EST|FROM|TO))\s)?((?:\d{0,2}\s\D{3}\s\d{1,4})|(?:\d{1,2}\.\d{1,2}\.\d{1,4})|\d{1,4})\s?((?:AND|TO))?\s?((?:\d{0,2}\s\D{3}\s\d{1,4})|(?:\d{1,2}\.\d{1,2}\.\d{1,4})|\d{1,4})?"
The goal wasn't to use other data formats then.
I forgot about this and try to use these formats.
I don't know how to fix the pattern after a long time.
Thanks to everyone who helped!
 
Upvote 0
Top