Str$ = DatePart("d", Date) & "." & DatePart("m", Date) & "." & DatePart("yyyy", Date)
Str$ = Day(Date) & "." & Month(Date) & "." & Year(Date)
Format$(Date, "dd\.mm\.yyyy")
A.A.Z. писал(а):skiperski, с возвращением!
A.A.Z. писал(а):Только вот в eVB нету функции Format
Public Function LZero(ByVal Number As Long, ByVal Length As Long) As String
LZero = Number
If (Len(LZero) < Length) Then LZero = Right$(String$(Length, "0") & Number, Length)
End Function
Public Function FormatDate(ByVal d As Date) As String
FormatDate = LZero(Day(d), 2) & "." & LZero(Month(d), 2) & "." & LZero(Year(d), 4)
End Function
Public Function FormatDateEx(ByVal d As Date, Optional ByVal mask As String = vbNullString, Optional ByVal FirstDayOfWeek As VbDayOfWeek = vbUseSystemDayOfWeek) As String
Dim i As Long 'counter
Dim ch As String 'single character
Dim s As String 'buffer
Dim state As Long 'current state
Dim state0 As Long 'state of previous step
Dim doFormat As Boolean 'flag
'state values
'0 - start
'10 - days "d"
'20 - month "m"
'30 - year "y"
'40 - week "w"
'50 - second "s"
'60 - minute "n"
'70 - hour "h"
'80 - time "ttttt"
'99 - any other symbole
'100 - end
If (Len(mask) = 0) Then mask = "ddddd ttttt" 'by default
For i = 1 To Len(mask) + 1
ch = Mid$(mask, i, 1) 'read char
state0 = state 'save previous state
Select Case LCase$(ch)
Case ""
doFormat = True
state = 100
Case "d"
If (state > 0 And state <> 10) Then doFormat = True
state = 10
Case "m"
If (state > 0 And state <> 20) Then doFormat = True
state = 20
Case "y"
If (state > 0 And state <> 30) Then doFormat = True
state = 30
Case "w"
If (state > 0 And state <> 40) Then doFormat = True
state = 40
Case "s"
If (state > 0 And state <> 50) Then doFormat = True
state = 50
Case "n"
If (state > 0 And state <> 60) Then doFormat = True
state = 60
Case "h"
If (state > 0 And state <> 70) Then doFormat = True
state = 70
Case "t"
If (state > 0 And state <> 80) Then doFormat = True
state = 80
Case Else
If (state > 0 And state <> 99) Then doFormat = True
state = 99
End Select
If (doFormat) Then
Select Case state0
Case 10
Do While (Len(s))
If (Len(s) > 5) Then 'dddddd - long date
FormatDateEx = FormatDateEx & FormatDateTime(d, vbLongDate)
s = Mid$(s, 7)
ElseIf (Len(s) > 4) Then 'ddddd - short date
FormatDateEx = FormatDateEx & FormatDateTime(d, vbShortDate)
s = Mid$(s, 6)
ElseIf (Len(s) > 3) Then 'dddd - long weekday name
FormatDateEx = FormatDateEx & WeekdayName(Weekday(d, FirstDayOfWeek), False, FirstDayOfWeek)
s = Mid$(s, 5)
ElseIf (Len(s) > 2) Then 'ddd - short weekday name
FormatDateEx = FormatDateEx & WeekdayName(Weekday(d, FirstDayOfWeek), True, FirstDayOfWeek)
s = Mid$(s, 4)
Else 'dd or d - day number with or without leading zero
FormatDateEx = FormatDateEx & LZero(Day(d), Len(s))
s = vbNullString
End If
Loop
Case 20
Do While (Len(s))
If (Len(s) > 3) Then 'mmmm - long month name
FormatDateEx = FormatDateEx & MonthName(Month(d), False)
s = Mid$(s, 5)
ElseIf (Len(s) > 2) Then 'mmm - short month name
FormatDateEx = FormatDateEx & MonthName(Month(d), True)
s = Mid$(s, 4)
Else 'mm or m - month number with or without leading zero
FormatDateEx = FormatDateEx & LZero(Month(d), Len(s))
s = vbNullString
End If
Loop
Case 30
Do While (Len(s))
If (Len(s) > 3) Then 'yyyy - long year number
FormatDateEx = FormatDateEx & LZero(Year(d), 4)
s = Mid$(s, 5)
ElseIf (Len(s) > 1) Then 'yy - short year number
FormatDateEx = FormatDateEx & LZero(Right$(Year(d), 2), 2)
s = Mid$(s, 3)
Else 'y - days of beginning year
FormatDateEx = FormatDateEx & DateDiff("d", DateSerial(Year(d), 1, 0), d)
s = vbNullString
End If
Loop
Case 40
Do While (Len(s))
If (Len(s) > 1) Then 'ww - number of current week
FormatDateEx = FormatDateEx & DatePart("ww", d, FirstDayOfWeek)
s = Mid$(s, 3)
Else 'w - weekday number
FormatDateEx = FormatDateEx & Weekday(d, FirstDayOfWeek)
s = vbNullString
End If
Loop
Case 50
Do While (Len(s))
If (Len(s) > 1) Then 'ss - seconds with leading zero
FormatDateEx = FormatDateEx & LZero(Second(d), 2)
s = Mid$(s, 3)
Else 's - seconds without leading zero
FormatDateEx = FormatDateEx & Second(d)
s = vbNullString
End If
Loop
Case 60
Do While (Len(s))
If (Len(s) > 1) Then 'nn - minutes with leading zero
FormatDateEx = FormatDateEx & LZero(Minute(d), 2)
s = Mid$(s, 3)
Else 'm - minutes without leading zero
FormatDateEx = FormatDateEx & Minute(d)
s = vbNullString
End If
Loop
Case 70
Do While (Len(s))
If (Len(s) > 1) Then 'hh - hours with leading zero
FormatDateEx = FormatDateEx & LZero(Hour(d), 2)
s = Mid$(s, 3)
Else 'h - hours without leading zero
FormatDateEx = FormatDateEx & Hour(d)
s = vbNullString
End If
Loop
Case 80
Do While (Len(s))
If (Len(s) > 4) Then 'ttttt - long time
FormatDateEx = FormatDateEx & FormatDateTime(d, vbLongTime)
s = Mid$(s, 6)
Else 'nothing
FormatDateEx = FormatDateEx & s
s = vbNullString
End If
Loop
Case Else 'other chars
FormatDateEx = FormatDateEx & s
End Select
s = vbNullString 'reset buffer
doFormat = False 'reset flag
End If
s = s & ch 'add char in buffer
Next
End Function
A.A.Z. писал(а):Можно по-ламерски
- Код: Выделить всё
Str$ = DatePart("d", Date) & "." & DatePart("m", Date) & "." & DatePart("yyyy", Date)
Arioh писал(а):Thanx! Все заработало
skiperski писал(а):Arioh писал(а):Thanx! Все заработало
1. Проверь дату, например: 01.02.2004. В этой реализации получишь "1.2.2004", а не как ожидалось "01.02.2004".
2. Гипотетическая ситуация: последние миллисекунды последнего дня месяца, например 31.10.2004. Первый вызов DatePart() отрабатывает сегодня, а второй и третий уже завтра. Получаем: 31.11.2004[/quote
Вернуться в Visual Basic для мобильных устройств
Сейчас этот форум просматривают: нет зарегистрированных пользователей и гости: 17