XOJOで日付を扱う

投稿者: | 2023年5月8日

もっと良い方法は多々あると思いますが日付や曜日を扱う場合に以下のような感じにしてます。
classとか使って再利用できるようにすればさらに便利に使えると思います。
以下参考までに

dim weekDayList() as string
weekDayList=array(“日”,”月”,”火”,”水”,”木”,”金”,”土”)

Dim d As Xojo.Core.Date = Xojo.Core.Date.Now
dim YYYY as Integer = d.Year
dim MM as Integer = d.Month
dim DD as Integer = d.Day
dim Ho as Integer = d.Hour
dim Mi as Integer = d.Minute
dim Se as Integer = d.Second
dim Ns as Integer = d.Nanosecond

System.DebugLog(d.ToText)

dim NowDate as String = str(YYYY) + “-” + right(“0” + str(MM),2) + “-” + Right(“0″ + str(DD),2)
dim NowDate2 as string = Format(YYYY,”0000”) + “/” + Format(MM,”00″) + “/” + Format(DD,”00″)
dim NowTime as string = Format(ho,”00″) + “:” + Format(Mi,”00″) + “:” + Format(Se,”00″)

dim youbi as string = weekDayList(d.DayOfWeek-1)

tf_day.Text=NowDate
TF_day2.Text=NowDate2
TF_TIME.text=NowTime
TF_DAYofWeek.Text=youbi

dim lastday as integer = getLastDayOfaMonth(MM,YYYY)
TF_LastDay.Text=lastday.ToString

//日付計算のテスト
Var di As New Xojo.Core.DateInterval
dim i as integer

for i=-30 to 30
  di.Days=i
  Var future As Xojo.Core.Date = Xojo.Core.Date.Now + di
  dim FD as String = str(future.Year) + “-” + right(“0” + str(future.Month),2) + “-” + Right(“0” + str(future.Day),2)
  System.DebugLog(i.ToString + “>>” +FD)
 
next i

 

上記サンプル中のgetLastDayOfaMonth
指定年月の最終日(Integer)を得るFunction

——–

Public Function getLastDayOfaMonth(pM as integer, pY as integer) As integer
  ‘pM=月
  ‘pY=年(西暦)
  ‘戻り値:pM月の月末日
  Dim monthDays As Integer
 
  Select Case pM
  Case 1, 3, 5, 7, 8, 10, 12
    monthDays = 31
  Case 4, 6, 9, 11
    monthDays = 30
  Case Else
    // Calculate February
    If (pY / 4.0) = Floor(pY / 4.0) Then
      monthDays = 29 // Leap year
    Else
      monthDays = 28
    End If
  End Select
 
  return(monthDays)
End Function

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です