Gặp rắc rối về ngày tháng

Liên hệ QC

vtt

Thành viên mới
Tham gia
19/12/06
Bài viết
38
Được thích
29
Xin các anh chị giúp dùm tôi việc này nhé.
1. Giả sử ô A1 được format (Menu Format -> Cells) ở dạng: dd/mm/yy
2. Start -> Settings -> Control Panel-> Regional and Language Options cũng định dạng ngày là: dd/mm/yy
3. Text box txbNgay được tôi nhập vào giá trị 05/06/07 (ngày 5 tháng 6 năm 2007).
4. Khi viết lệnh cells(1,1).value=txbNgay.value thì ô A1 lại nhận được giá trị là 06/05/07 (ngày 6 tháng 5). Khi kiểm tra lại bằng hàm sẽ thấy rõ điều này
day(A1)=6
month(A1)=5

Vì sao tôi lại bị lỗi như vậy?
 

File đính kèm

  • rac roi ngay thang.xls
    20 KB · Đọc: 35
vtt đã viết:
Xin các anh chị giúp dùm tôi việc này nhé.
1. Giả sử ô A1 được format (Menu Format -> Cells) ở dạng: dd/mm/yy
2. Start -> Settings -> Control Panel-> Regional and Language Options cũng định dạng ngày là: dd/mm/yy
3. Text box txbNgay được tôi nhập vào giá trị 05/06/07 (ngày 5 tháng 6 năm 2007).
4. Khi viết lệnh cells(1,1).value=txbNgay.value thì ô A1 lại nhận được giá trị là 06/05/07 (ngày 6 tháng 5). Khi kiểm tra lại bằng hàm sẽ thấy rõ điều này
day(A1)=6
month(A1)=5

Vì sao tôi lại bị lỗi như vậy?

Bạn xem lại nhé. Có gì hỏi tiếp.

Thân !
 

File đính kèm

  • rac roi ngay thang.7z
    9.1 KB · Đọc: 64
Mr Okebab đã viết:
Bạn xem lại nhé. Có gì hỏi tiếp.
Thân !
Xin đề nghị (Các bác đừng giận): Trình bày rõ ý tưởng và nội dung.
Lý do:
- Người đọc dễ hiểu và không phải download.
- Tiết kiệm tài nguyên upload.
 
Theo tôi chỉ cần dòng lệnh như sau là OK, đưa tất cả về theo ngày Fren, dù txbngay nhập dạng gì.
Private Sub CommandButton1_Click()
Cells(1, 1).Value = DateSerial(Year(TxbNgay), Month(TxbNgay), Day(TxbNgay))
End Sub
 
Cảm ơn rất nhiều.
Cách của ThuNghi rất hay, cách của Mr Okebab cũng vậy.
Mình nghĩ nên kết hợp 2 cách này lại sẽ hay hơn như sau:

If Not IsDate(txbNgay.Value) Then
MsgBox "Ngay khong hop le"
Else
Cells(1, 1).Value = DateSerial(Year(txbNgay), Month(txbNgay), Day(txbNgay))
End If

Nếu bạn nào muốn tìm hiểu thêm về hàm DateSerial thì đọc đoạn này nhé (tôi đọc xong cũng chả hiểu gì mấy :., )


DateSerial Function

Returns a Variant (Date) for a specified year, month, and day.

Syntax

DateSerial(year, month, day)

The DateSerial function syntax has these named arguments:

Part Description
year Required; Integer. Number between 100 and 9999, inclusive, or a numeric expression.
month Required; Integer. Any numeric expression.
day Required; Integer. Any numeric expression.



Remarks

To specify a date, such as December 31, 1991, the range of numbers for each DateSerial argument should be in the accepted range for the unit; that is, 1–31 for days and 1–12 for months. However, you can also specify relative dates for each argument using any numeric expression that represents some number of days, months, or years before or after a certain date.

The following example uses numeric expressions instead of absolute date numbers. Here the DateSerial function returns a date that is the day before the first day (1 - 1), two months before August (8 - 2), 10 years before 1990 (1990 - 10); in other words, May 31, 1980.

DateSerial(1990 - 10, 8 - 2, 1 - 1)

Under Windows 98 or Windows 2000, two digit years for the year argument are interpreted based on user-defined machine settings. The default settings are that values between 0 and 29, inclusive, are interpreted as the years 2000–2029. The default values between 30 and 99 are interpreted as the years 1930–1999. For all other year arguments, use a four-digit year (for example, 1800).

Earlier versions of Windows interpret two-digit years based on the defaults described above. To be sure the function returns the proper value, use a four-digit year.

When any argument exceeds the accepted range for that argument, it increments to the next larger unit as appropriate. For example, if you specify 35 days, it is evaluated as one month and some number of days, depending on where in the year it is applied. If any single argument is outside the range -32,768 to 32,767, an error occurs. If the date specified by the three arguments falls outside the acceptable range of dates, an error occurs.

Note For year, month, and day, if the Calendar property setting is Gregorian, the supplied value is assumed to be Gregorian. If the Calendar property setting is Hijri, the supplied value is assumed to be Hijri.

The returned date part is in the time period units of the current Visual Basic calendar. For example, if the current calendar is Hijri and the date part to be returned is the year, the year value is a Hijri year. For the argument year, values between 0 and 99, inclusive, are interpreted as the years 1400-1499. For all other year values, use the complete four-digit year (for example, 1520).
 
Web KT
Back
Top Bottom