[บทเรียน, B4A]การกำหนดรูปแบบของวันเดือนปี

Theera

Well-Known Member
Licensed User
Longtime User
 

BlueVision

Active Member
Licensed User
Longtime User
It depends on where the value for the date comes from. Do you read the value from somewhere or do you compile the value yourself?

If the value comes from "external" in this form, you should calculate the corresponding tick value from it. Note that the tick value also contains a time component and not just the date.

- first define the date format with Date format
- calculate the tick value and then use DAY OF THE WEEK to determine the day of the week (you will get a numerical value between 0 and 7, to which you can then assign the days of the week depending on the START OF WEEK value)
- then form the corresponding date from the tick value and combine both strings for the display in your programme
 

Theera

Well-Known Member
Licensed User
Longtime User
It depends on where the value for the date comes from. Do you read the value from somewhere or do you compile the value yourself?

If the value comes from "external" in this form, you should calculate the corresponding tick value from it. Note that the tick value also contains a time component and not just the date.

- first define the date format with Date format
- calculate the tick value and then use DAY OF THE WEEK to determine the day of the week (you will get a numerical value between 0 and 7, to which you can then assign the days of the week depending on the START OF WEEK value)
- then form the corresponding date from the tick value and combine both strings for the display in your programme
Although I have tried to translate, but I acepted myself, I don't understand at all. I
still need the examples for learning.
 

BlueVision

Active Member
Licensed User
Longtime User
Try this piece code, maybe it gives you an idea. I wrote it just down and tried in B4J. It's B4X-Code, should adapt to B4A.
It sets the timeformat, defines an array of strings to hold the names of the days of the week. Then it reads the actual ticks value and composes the logline.
A quick and dirty hack for giving you an idea how it works.
You can modify the shown values with numberformat to "bring it in shape".

If you reading out a date from somewhere else (maybe a given database), you have to bring it in shape before. This here works only for the actual date (datetime.now)

B4X:
Private Sub Button1_Click
    DateTime.DateFormat = "dd.MM.yyyy"
    Dim DayName() As String
    DayName = Array As String ("", "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa")
    Dim Actual As Double = DateTime.Now
    Log (DayName(DateTime.GetDayOfWeek(Actual)) & ", " & DateTime.GetDayOfMonth(Actual) & "." & DateTime.GetMonth(Actual) & "." & DateTime.GetYear(Actual))
End Sub

Create a new project, define a button. Every click on the button should create a message in the log.
 

Theera

Well-Known Member
Licensed User
Longtime User
I've tested as your following. Thank you for kind of you,BlueVision. I don't know about his format "ddd,dd.MM.yyyy" I can't explain to Thai developlers what is that.
 

Attachments

  • TestDate.zip
    9.1 KB · Views: 19

BlueVision

Active Member
Licensed User
Longtime User
You are very welcome. Not viewed your zip, posting this here with my mobile.

Each abbreviation stands for a defined term. NOTE THE DIFFERENCE WHEN USING UPPERCASE AND LOWERCASE LETTERS!
ddd makes no sense. It would represent the day in month (only two characters are needed, so "dd" would make sense).
DDD makes sense, stands for the day of the year (three characters).
Maybe this table is helpful for you? ©

Letter​
Date or Time Component​
Presentation​
Examples​
GEra designatorTextAD
yYearYear1996; 96
YWeek yearYear2009; 09
MMonth in yearMonthJuly; Jul; 07
wWeek in yearNumber27
WWeek in monthNumber2
DDay in yearNumber189
dDay in monthNumber10
FDay of week in monthNumber2
EDay name in weekTextTuesday; Tue
uDay number of week (1 = Monday, ..., 7 = Sunday)Number1
aAm/pm markerTextPM
HHour in day (0-23)Number0
kHour in day (1-24)Number24
KHour in am/pm (0-11)Number0
hHour in am/pm (1-12)Number12
mMinute in hourNumber30
sSecond in minuteNumber55
SMillisecondNumber978
zTime zoneGeneral time zonePacific Standard Time; PST; GMT-08:00
ZTime zoneRFC 822 time zone-0800
XTime zoneISO 8601 time zone-08; -0800; -08:00
 

BlueVision

Active Member
Licensed User
Longtime User
Tested your program. Works fine. And you fixed my bugs (your variable declaration long is correct, I was wrong, who cares, it was a quick and dirty hack this morning to support you).
Is this what you wanted?

You understood the thing with the ticks? The ticks value is a simple counter, started at 1.1.1970 0:00 GMT. Since then, it counts up +1 every millisecond.
So with DateTime.Now you can get the actual tick from your mobile and with all the other functions you can extract everything from it.
It becomes tricky, when you have to interpret a given time without ticks (coming from a database or whatever). Then you have to do a lot more work and to parse the given string. But B4X (B4A) will help you with strong parsing tools. Search for examples.
Simply enter DateTime. in your code and you will see the options.

DateTime.GetDayOfYear will give you probably that strange DDD-value (written in your post as "ddd"), a value between 1 and 366.

Ticks calculations make it very simple to calculate the intervals between two dates. You don't have to care about the length of a month, or leap day in february.
Keep in mind, this all is based on the gregorian calender, a de-facto worldwide standard. It may become eventually tricky to convert this to Suriyakati (traditional thai), Hidschra-Counting (islamic) or the hebrew calendar.
 

Theera

Well-Known Member
Licensed User
Longtime User
Hi BlueVision,
Many thanks for your kind of you again. Your explain help Thai develplers understand more.
 
Top