Good morning
I would like to use Xojo’s native DateTimePicker. I would like to click on a date and display it on a textfields , but I can’t. I wrote the following code.
Var theDate As DateTime
theDate = datetimepicker1.SelectedDate
Textfield1.Text=datetimepicker1.SelectedDate
But return an error when display date in last row.
I have two questions if possible, can you format the date that comes out as a short date like (06/05/2022)? And can the DateTimePicker calendar boxes be colored?
Yes. Look at the help documentation for DateTime.ToString and you will see this example:
dateStyle As DateTime.FormatStyles = DateTime.FormatStyles.Medium, timeStyle As DateTime.FormatStyles = DateTime.FormatStyles.Medium
Medium
The medium date or time format
Dec 1, 2022 - 12:23:06 PM
Var d As DateTime = DateTime.Now
Var s As String = d.ToString(Locale.Current, DateTime.FormatStyles.Short, DateTime.FormatStyles.None)
// s = 10/31/17 (this varies by date and your system locale settings)
’ Utilizzo il datetimePicker ed acquisisco la data selezionata
Var TheDateSelected As DateTime = datetimepicker1.SelectedDate
'Converto la data selezionata in SHORT
Var DataSelectedShort As String = TheDateSelected.ToString(Locale.Current, DateTime.FormatStyles.Short, DateTime.FormatStyles.None)
'La visualizzo
Textfield1.Text= DataSelectedShort
MORE QUESTIONE: Is it possible to color the datetimepicker cells differently?
Nothing built in to the date time pickler itself
You may be able to using declares on different platforms
That would require knowing what platform your on and then some investigation of what native control is used on each platform - and it may not be a native control everywhere
Ok in fact I could not find anything reading the info on the DateTimePicker. And there is the possibility to understand the day of the beginning of the month.
Sure the first day of any month is fairly easy to obtain using a date time instance
var year as integer = 2022
var month as integer = 5
var firstDayOfMonth as new DateTime( year, month, 1)
// firstDayOfMonth is now a date time instance that holds the date of the first day of the month
DateTime.DayofWeek returns an integer representing the day of the week (where Sunday is day 1). You’ll have to map the days as integer to day as string yourself. A simple Select statement can do it.
Var dayAsString as string
var firstDayOfMonth as new DateTime( year, month, 1)
Select firstDayOfMonth.DayOfWeek
Case 1
dayAsString = "Sunday"
Case 2
' the rest is left as an exercise for the reader
...
HOWEVER note that what you get will vary from one system to another because system settings DO affect what is returned for the different format styles
So even if you were to do something like
Var year As Integer = 2022
Var month As Integer = 5
Var firstDayOfMonth As New DateTime( year, month, 11)
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)
Break
this will ONLY work in locales & systems where the day name IS written first
On others, if they write the full date as Month, DayName, Year it will fail
Theres some ancient code on my web site that tries to get the names for many languages
Public Function LocalizedDayFromDate(mDate as datetime) As string
#If TargetMacOS
Declare Function NSClassFromString Lib "Cocoa" (clsName As CFStringRef) As ptr
Declare Function currentLocale Lib "Cocoa" Selector "currentLocale" (clsRef As ptr) As ptr
Var currentLocale As ptr = currentLocale(NSClassFromString("NSLocale"))
Declare Function alloc Lib "Cocoa" Selector "alloc" (clsRef As ptr) As ptr
Declare Function init Lib "Cocoa" Selector "init" (obj_id As ptr) As ptr
Var DateFormatter As ptr = init(alloc(NSClassFromString("NSDateFormatter")))
declare sub setLocale lib "Cocoa" selector "setLocale:" (obj_id as ptr, locale as ptr)
Declare Sub setDateFormat Lib "Cocoa" Selector "setDateFormat:" (obj_id As ptr, Format As CFStringRef)
Dim nsdateformater As ptr = DateFormatter
setLocale(nsdateformater,CurrentLocale)
setDateFormat(nsdateformater,"cccc")
Declare Function stringFromDate Lib "Cocoa" Selector "stringFromDate:" (obj_id As ptr, date As ptr) As CFStringRef
Declare Function dateWithTimeIntervalSince1970 Lib "Cocoa" Selector "dateWithTimeIntervalSince1970:" (class_id As Ptr, seconds As Double) As Ptr
Dim seconds As Double = mDate.SecondsFrom1970
Var dateWithInterval As ptr = dateWithTimeIntervalSince1970(NSClassFromString( "NSDate" ), seconds)
Dim result As CFStringRef = stringFromDate(nsdateformater, dateWithInterval)
Declare Function capitalizedString Lib "Cocoa" Selector "capitalizedString" (Str As CFStringRef) As CFStringRef
Return capitalizedString(result)
#Else
return ""
#EndIf
End Function
I write this:
Var TheDateSelected As DateTime = datetimepicker1.SelectedDate
Var DataSelectedShort As String = TheDateSelected.ToString(Locale.Current, DateTime.FormatStyles.Short, DateTime.FormatStyles.None)
Var dayAsString as string = DataSelectedShort
var firstDayOfMonth as new DateTime(year, month, 1)
Select firstDayOfMonth.DayOfWeek
Case 1
dayAsString = “Domenica”
Case 2
dayname = “Lunedi”
Case 3
dayname = “Martediy”
Case 4
dayname = “Mercoledi”
Case 5
dayname = “Giovedi”
Case 6
dayname = “Venerdi”
Case 7
dayname = “Sabato”
End Select
messagebox dayAsString
Bur return error “var firstDayOfMonth as new DateTime(year, month, 1)
Select firstDayOfMonth.DayOfWeek” ,where am I wrong
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)
messagebox(dayname)
in the end I used your method. using datapicker reference, I see the month and see the first day of the month extracted from the read date with Var dayname As String = dateString.NthField ("", 1)
.