Android Question Days of the week

rogel a. tolentino

Member
Licensed User
To determine the day of the week of a current date is Datetime.getDayOfWeek(DateTime.now)
how about not a current date
example April 27, 2019
 

DonManfred

Expert
Licensed User
It is working fine her

B4X:
    Dim SomeTime As Long
    SomeTime = DateTime.DateParse("04/27/2019")
    Log(SomeTime)
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
1556316000000
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
 

Attachments

RWK

Member
Licensed User
B4X:
    Dim WeekDay    As Int
    Dim WeekDayLong() As String = Array As String("ErelsDay","Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
    WeekDay = DateTime.GetDayOfWeek(DateTime.DateParse("04/28/2019"))
    Log (WeekDayLong(WeekDay))
Greetings
Rainer
 

ANGELINE ROMAN

Member
Licensed User
I already understand dateparse
for example a given date stored in a string variable
say date1="04/27/2019"
when you add 3 days the new date is "04/30/2019"
can somebody help me a b4a code that will display the original date and the new date
both date in mm/dd/yyyy format
Thanks
 

emexes

Well-Known Member
Licensed User
This example:
B4X:
Dim Date1 As Long    'milliseconds since year dot
Dim Date2 As Long

DateTime.DateFormat = "MM/dd/yyyy"    'is default, but best to be sure

Date1 = DateTime.DateParse("04/27/2019")
Date2 = DateTime.Add(Date1, 0, 0, 3)    'add 3 days

Log("First Date = " & DateTime.Date(Date1))
Log("Second Date = " & DateTime.Date(Date2))
generates this log (but you could just as easily put the strings to a label as to the log):
B4X:
First Date = 04/27/2019
Second Date = 04/30/2019
edit: removed date-time arithmetic alternative, because it gets messy with daylight savings / summer time adjustments
 
Last edited:

emexes

Well-Known Member
Licensed User
reverting back to the original weekday question, another way of doing it might be:
B4X:
Dim SaveDateFormat As String = DateTime.DateFormat

DateTime.DateFormat = "yyyy-MM-dd"
Dim DateOfInterest As Long = DateTime.DateParse("2019-04-27")

DateTime.DateFormat = "EEEE"
Log("as weekday = " & DateTime.Date(DateOfInterest))

DateTime.DateFormat = "EEEE MM/dd/yy"
Log("as weekday and date = " & DateTime.Date(DateOfInterest))

DateTime.DateFormat = "EEE MM/dd/yy"
Log("as short weekday and date = " & DateTime.Date(DateOfInterest))

DateTime.DateFormat = SaveDateFormat    'restore to original format
Log("as original format = " & DateTime.Date(DateOfInterest))
generates results:
B4X:
as weekday = Sunday
as weekday and date = Sunday 04/27/19
as short weekday and date = Sun 04/27/19
as original format = 04/27/2019
 

emexes

Well-Known Member
Licensed User
This is a mistake. Date and time arithmetics do not work.
That's a bit harsh :)

Although I'm coming at it from a physics-time point of view, but you're right that from a clock-time point of view, I should have added a caveat that things will go a bit awry twice a year when daylight savings/summer time kicks in or out (although curiously, leap days are accounted for but leap seconds are not - what's that about?!?!)

Given that the original question was about whole days and regardless of their length, I've rectified my post above.
Watch the date and time video tutorial for more information: https://www.b4x.com/etp.html
I did watch this, and it was great - I hadn't encountered that one-hour-is-three-hours time zone trap before, but as soon as you demonstrated it, I thought: man, I bet you must have explained that a few hundred times, no wonder you didn't hold back on declaring my arithmetic to be a mistake ;-)

Use DateUtils.AddPeriod instead.
I assume DateTime.Add is still ok to use.
 
Top