DateTimePicker

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.

A DateTime is an object that isn’t a string. You can have it treated as a string by adding .ToString to the statement. Use

Textfield1.Text=datetimepicker1.SelectedDate.ToString

Ok, of course, it works :slight_smile:

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)

Ok i solved for short date with this code:

’ 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.

For example May 2022, first day is Sunday.

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

var year as integer = 2022
var month as integer = 5
var firstDayOfMonth as new DateTime( year, month, 11)

messagebox firstDayOfMonth.ToString

If i write this, retund the Date, non the name of the day…in this case Wednesday

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
...

The day name is specific to the locale
They are different in different languages

There are options you can set to affect how the conversion to string is done
see DateTime.ToString - Xojo Documentation

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

Such fun !

IF you are on macOS here’s a useful bit

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

I’ll guess the error is “year doesn’t exist” or something like that
Same for month

reading those errors closely can tell you what to go look at

you would d need to look at the line its referring to and see “what’ is it I’m trying to do ?”

I want the first day of the month for the date they selected

so why not somehow use TheDateSelected and take its year & month ?

should probably be

var firstDayOfMonth as new DateTime(TheDateSelected.year, TheDateSelected.month, 1)

FWIW learning how to figure out the problem you have and how to solve it is probably the most useful thing to spend time learning

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)
.

It seems it works :slight_smile: