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
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
you need to break the string apart into its pieces
convert each piece to an integer
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))
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
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.
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