Problem with date format

Good evening group, I have a problem with dates. If I acquire the current date with DateTime.Now, I am able to use it, I display it as short and I extrapolate the year and month from it. Now instead I should do the opposite, acquire the date from a txtfield called textDataAttuale and which displays the date in the format dd / mm / YY, to extrapolate the same year and month. I write a piece of code:

Var TheDateSelected As DateTime = DateTime.Now
Var DataSelectedShort As String = TheDateSelected.ToString(Locale.Current, DateTime.FormatStyles.Short, DateTime.FormatStyles.None)
textDataAttuale.Text= DataSelectedShort

Dim year as integer
Dim month as integer
year=TheDateSelected.Year
month=TheDateSelected.Month

just so you know if you put you code inside certain markers it will show as code
you can use 3 back ticks
```
before & after your code OR the

[code] 
your text here 
[/code]``` 
markers

Ok sorry, I corrected the problem

Its not really a “problem” but it does make the code stand out better

If you have a nicely formatted date in a string from a text field then

  1. you need to break the string apart into its pieces
  2. convert each piece to an integer
  3. use those integers to create a new instance of the DateTime class

There is NO error checking of any kind in the following
And there are errors that can occur like a person typing in a string like abbcekjdh
This is an example of the major steps you’d need


Dim year As Integer
Dim month As Integer
Dim day As Integer

// assuming the text input is dd / mm / YYYY

// 1) you need to break the string apart into its pieces
//     and trim out any spaces as they can mess the conversion to integer up
Dim parts() As String = Split( input_date.ReplaceAll(" ",""), "/")

// normally you'd want code to check that there are 3 parts etc

// 2) convert each piece to an integer
day = parts(0).ToInteger
month = parts(1).ToInteger
year = parts(2).ToInteger

// 3) use those integers to create a new instance of the DateTime class
return new DateTime(year, month,day)
I did so, is it the same?
Dim year as integer
Dim month as integer
Dim Days as integer

year= integer.FromString(Right(textDataAttuale.Text,2))        'TheDateSelected.Year
month=integer.fromstring(Mid(textDataAttuale.Text, 4,2))

that is another way to accomplish the same thing

OK perfect. But I have a problem … with the first code I get the first day of the month wrong. With the second code I get the exact one. Where am I wrong in the first code?

calendario.RemoveAllRows

Dim input_date as string
input_date=textDataAttuale.Text    'READ DATE FROM MTXTFIELD

Dim year as integer
Dim month as integer
Dim Days as integer

Dim parts() As String = Split(input_date.ReplaceAll(" ",""), "/")
month = parts(1).ToInteger
year = parts(2).ToInteger

messagebox "Anno2= " + year.ToString
messagebox "Mese2 " + MONTH.ToString


if year mod 4 <> 0 then
  
  select case month
  case 1,3,5,7,8,10,12
    Days= 31
  case 4,6,9,11
    Days= 30
  case 2
    if year mod 4 <> 0 then
      Days= 28
    else
      Days= 29
    end if
  end select
end if
' OTTENGO QUANTI GIORNI HA IL MESE SELEZIONATO in Days


'MessageBox  " IL MESE " + month.ToString + " AVRa " + Days.ToString + " GIORNI"

' ed ora ricavo altre info x la tabella
Var firstDayOfMonth As New DateTime( year, month, 1)  'Primo giorno delll'anno X e del mese X
Dim currentLocale As Locale = Locale.Current
Var dateString As String = firstDayOfMonth.ToString(currentLocale, DateTime.FormatStyles.Full, DateTime.FormatStyles.None )
Var dayname As String = dateString.NthField(" ", 1)   'dateString.NthField(",", 1)
Dim SpostamentoPrimoGiorno as integer


messagebox "Mese " + month.ToString + " ha " + Days.ToString +" giorni" + "Il primo giorno "+ dayname



>>>>>>>> ERROR    dayname=VENERDI

' Utilizzo il datetimePicker ed acquisisco la data selezionata
Var TheDateSelected As DateTime = DateTime.Now
'Converto la data selezionata in SHORT
Var DataSelectedShort As String = TheDateSelected.ToString(Locale.Current, DateTime.FormatStyles.Short, DateTime.FormatStyles.None)
'La visualizzo
textDataAttuale.Text= DataSelectedShort



Dim year as integer
Dim month as integer
Dim Days as integer

year=TheDateSelected.Year
month=TheDateSelected.Month
if year mod 4 <> 0 then
  
  select case month
  case 1,3,5,7,8,10,12
    Days= 31
  case 4,6,9,11
    Days= 30
  case 2
    'Calcoliamo i giorni di Febraio
    if year mod 4 <> 0 then
      Days= 28
    else
      Days= 29
    end if
  end select
end if
' OTTENGO QUANTI GIORNI HA IL MESE SELEZIONATO in Days



' ed ora ricavo altre info x la tabella
Var firstDayOfMonth As New DateTime( year, month, 1)  'Primo giorno delll'anno X e del mese X
Dim currentLocale As Locale = Locale.Current
Var dateString As String = firstDayOfMonth.ToString(currentLocale, DateTime.FormatStyles.Full, DateTime.FormatStyles.None )
Var dayname As String = dateString.NthField(" ", 1)   'dateString.NthField(",", 1)
Dim SpostamentoPrimoGiorno as integer


messagebox "Mese " + month.ToString + " ha " + Days.ToString +" giorni" + "Il primo giorno + "+ dayname

>>>>>>>>> CORRECT   dayname=domenica

Have you looked at the DateTimePicker UI control?

not sure
here I hard coded a date - by making a couple small changes so I could test

// calendario.RemoveAllRows

Dim input_date As String
input_date = "2/4/2020"

and that tells me that indeed April 2020 started on Wednesday

except for the code figuring out what yer & month the rest is the same so I really don’t see how you code is wrong

I don’t use DateTimePicker anymore

2/4/2020 Wednesday (OK)
In the code:
messagebox “Month” + month.ToString + “ha” + Days.ToString + “days” + “The first day” + dayname
Month 4 has 0 days The first day Wednesday
(0 days? WHY?)

6/5/2022
Month 5 has 31 days on the first day Sunday (OK)

if i read from txtfields the date
I get that:
Month 5 has 31 days on the first day Friday (WRONG)

I’m sure I was wrong somewhere, but I don’t understand where.

I Link progect.TEST CALENDAR.xojo_binary_project | Files.fm.

Your Days = section is in Select Case and before that you have:

if year mod 4 <> 0 then

2020 mod 4 = 0 so the Select Case is not processed.


image

Maybe you entered 6/5/2020?

Ok Mr. I solved !!! Thanks

In this section of your code, I always get an error because you are converting the datetime to String and your computer uses Italian and mine English:

Select Case dayname
  
Case "lunedì"
  SpostamentoPrimoGiorno=0
Case "martedì"
  SpostamentoPrimoGiorno=1
Case "mercoledì"
  SpostamentoPrimoGiorno=2
Case "giovedì"
  SpostamentoPrimoGiorno=3
Case "venerdì"
  SpostamentoPrimoGiorno=4
Case "sabato"
  SpostamentoPrimoGiorno=5
Case "domenica"
  SpostamentoPrimoGiorno=6
Else
  messagebox " Attenzione - possibile errore nel calendario"
End Select

You can change the above code to:

SpostamentoPrimoGiorno = if(firstDayOfMonth.DayOfWeek-2 = -1, 6, firstDayOfMonth.DayOfWeek-2)

and keep the logic.

Ok but i solved, the date work correctly…

Perfect, just wanted to let you know that your program is not international aware, if the system is in English it will cause problems.

No problem if your app is for you.

1 Like

Thanks, yes is for me only :slight_smile: I work and i trasforn evrey date in short, work correctly :slight_smile: Thanks.