• Nhờ kiểm tra code
  • Nhờ kiểm tra code

    tranthanhan1962 > 21-03-23, 09:30 PM

    Mình nhờ Chat GPT viết code và được trả lời như dưới đây. Nhờ các bạn kiêm tra hộ:
    Mã PHP:
    Save my prompt

    All prompts

    tạo 1 đoạn mã vba chuyển ngày dương lịch thành ngày âm lịch
    Use active language
    Incorporate keywords
    Provide a call to action
    Merlin Says
    :
    Để chuyển đổi ngày dương lịch thành ngày âm lịch trong VBAchúng ta có thể sử dụng các hàm tích hợp của Excel và một số biến đổi công thứcDưới đây là đoạn code mẫu:

    Function 
    Solar2Lunar(SolarDay As Date) As String
        Dim LunarDay 
    As String
        Dim LunarYear 
    As Long
        Dim LunarMonth 
    As Long
        Dim LunarDate 
    As Long
        Dim SpringStart 
    As Long
        Dim SpringMonth 
    As Long
        
        LunarDay 
    ""
        
        
    ' Tính ngày 8/2 Âm lịch năm âm lịch tương ứng với ngày dương lịch đầu vào
        LunarYear = Year(SolarDay)
        SpringStart = WorksheetFunction.EoMonth(SolarDay, -2) + 1
        If Day(SolarDay) < SpringStart Then
            LunarYear = LunarYear - 1
            SpringStart = WorksheetFunction.EoMonth(DateSerial(LunarYear, 2, 1), -2) + 1
        End If
        SpringMonth = Month(SpringStart)
        LunarDate = Day(SpringStart) + DateDiff("d", SpringStart, SolarDay) + 1
        
        ' 
    Tính tháng và ngày âm lịch tương ứng với ngày 8/2 Âm lịch
        LunarMonth 
    1
        
    Do While LunarMonth <= 12
            
    If LunarDate <= WorksheetFunction.EoMonth(DateSerial(LunarYearLunarMonth1), 0Then
                
    Exit Do
            End If
            LunarDate LunarDate WorksheetFunction.EoMonth(DateSerial(LunarYearLunarMonth1), 0)
            LunarMonth LunarMonth 1
        Loop
        
        
    ' Tạo chuỗi ngày âm lịch đầu ra
        LunarDay = LunarDate & "/" & LunarMonth & "/" & LunarYear
        
        Solar2Lunar = LunarDay
    End Function
    Để sử dụng, bạn chỉ cần gọi hàm Solar2Lunar với đối số là ngày dương lịch bạn muốn chuyển đổi, kiểu dữ liệu của đối số là Date, và hàm trả về chuỗi ngày âm lịch tương ứng. Ví dụ:

    Sub Test()
        Dim SolarDay As Date
        Dim LunarDay As String
        
        SolarDay = DateSerial(2021, 4, 7)
        LunarDay = Solar2Lunar(SolarDay)
        
        MsgBox LunarDay
    End Sub
    Kết quả khi chạy đoạn mã này là chuỗi "T2/3/2021", tương ứng với ngày 7/4/2021 trong lịch dương là ngày thứ ba, ngày 3 tháng 2 năm 2021 trong lịch âm.

    FREE
    45
    You still have 45 queries left! 
  • RE: Nhờ kiểm tra code

    ongke0711 > 21-03-23, 10:43 PM

    (21-03-23, 09:30 PM)tranthanhan1962 Đã viết: Mình nhờ Chat GPT viết code và được trả lời như dưới đây. Nhờ các bạn kiêm tra hộ:

    Code này nó cho kết quả sai và hiển thị cũng lung tung luôn.

    Mã:
    Function Solar2Lunar(solarDay As Date) As String
        Dim lunarDay As String, lunarYear As Long, lunarMonth As Long, LunarDate As Long
        Dim SpringStart As Long, SpringMonth As Long
       
        lunarDay = ""
       
        ' Tính ngày 8/2 Âm l?ch nam âm l?ch tuong ?ng v?i ngày duong l?ch d?u vào
        lunarYear = Year(solarDay)
        SpringStart = WorksheetFunction.EoMonth(solarDay, -2) + 1
        If Day(solarDay) < SpringStart Then
            lunarYear = lunarYear - 1
            SpringStart = WorksheetFunction.EoMonth(DateSerial(lunarYear, 2, 1), -2) + 1
        End If
        SpringMonth = Month(SpringStart)
        LunarDate = Day(SpringStart) + DateDiff("d", SpringStart, solarDay) + 1
       
        ' Tính tháng và ngày âm l?ch tuong ?ng v?i ngày 8/2 Âm l?ch
        lunarMonth = 1
        Do While lunarMonth <= 12
            If LunarDate <= WorksheetFunction.EoMonth(DateSerial(lunarYear, lunarMonth, 1), 0) Then
                Exit Do
            End If
            LunarDate = LunarDate - WorksheetFunction.EoMonth(DateSerial(lunarYear, lunarMonth, 1), 0)
            lunarMonth = lunarMonth + 1
        Loop
       
        ' T?o chu?i ngày âm l?ch d?u ra
        lunarDay = LunarDate & "/" & lunarMonth & "/" & lunarYear
        Solar2Lunar = lunarDay
       
    End Function
    'Ð? s? d?ng, b?n ch? c?n g?i hàm Solar2Lunar v?i d?i s? là ngày duong l?ch b?n mu?n chuy?n d?i, ki?u d? li?u c?a d?i s? là Date, và hàm tr? v? chu?i ngày âm l?ch tuong ?ng. Ví d?:

    Sub Test()
        Dim solarDay As Date
        Dim lunarDay As String
       
        solarDay = DateSerial(2021, 4, 7)
        lunarDay = Solar2Lunar(solarDay)
       
        MsgBox solarDay & " -- " & lunarDay
       
    End Sub

    'K?t qu? khi ch?y do?n mã này là chu?i "T2/3/2021", tuong ?ng v?i ngày 7/4/2021 trong l?ch duong là ngày th? ba, ngày 3 tháng 2 nam 2021 trong l?ch âm.


    Em hỏi bằng tiếng Anh thì nó cũng ra code nhưng cũng sai tè le.

    Mã:
    Function SolarToLunar(solarYear As Integer, solarMonth As Integer, solarDay As Integer) As String
        Dim lunarYear As Integer, lunarMonth As Integer, lunarDay As Integer
        Dim leapMonth As Integer, leapMonthDay As Integer
       
        ' The lunar calendar has a 60-year cycle, with the first year starting in 1984.
        ' The lunar year 1984 corresponds to the solar year 1924.
        Dim baseSolarYear As Integer: baseSolarYear = 1924
        Dim offset As Integer: offset = solarYear - baseSolarYear
       
        ' Calculate the number of days between the solar new year (Jan 1st) and the lunar new year.
        Dim daysToLunarNewYear As Integer: daysToLunarNewYear = 0
        For i = 1 To solarMonth - 1
            daysToLunarNewYear = daysToLunarNewYear + SolarDaysInMonth(solarYear, i)
        Next i
        daysToLunarNewYear = daysToLunarNewYear + solarDay - 1
       
        ' Calculate the lunar year and month.
        Dim daysInLunarYear As Integer: daysInLunarYear = LunarDaysInYear(offset)
        Do While daysToLunarNewYear >= daysInLunarYear
            daysToLunarNewYear = daysToLunarNewYear - daysInLunarYear
            offset = offset + 1
            daysInLunarYear = LunarDaysInYear(offset)
        Loop
        lunarYear = baseSolarYear + offset
       
        leapMonth = LunarLeapMonth(offset)
        lunarMonth = 1
        Do While lunarMonth < 13
            If leapMonth > 0 And lunarMonth = leapMonth + 1 Then
                leapMonthDay = LunarLeapMonthDays(offset)
                If daysToLunarNewYear >= leapMonthDay Then
                    daysToLunarNewYear = daysToLunarNewYear - leapMonthDay
                    lunarMonth = lunarMonth + 1
                Else
                    Exit Do
                End If
            End If
           
            Dim daysInLunarMonth As Integer: daysInLunarMonth = LunarDaysInMonth(offset, lunarMonth)
            If daysToLunarNewYear >= daysInLunarMonth Then
                daysToLunarNewYear = daysToLunarNewYear - daysInLunarMonth
                lunarMonth = lunarMonth + 1
            Else
                Exit Do
            End If
        Loop
       
        lunarDay = daysToLunarNewYear + 1
       
        SolarToLunar = Format(lunarYear, "0000") & "年" & Format(lunarMonth, "00") & "月" & Format(lunarDay, "00") & "日"
    End Function

    Function SolarDaysInMonth(solarYear As Integer, solarMonth As Integer) As Integer
        Select Case solarMonth
            Case 1, 3, 5, 7, 8, 10, 12
                SolarDaysInMonth = 31
            Case 4, 6, 9, 11
                SolarDaysInMonth = 30
            Case 2
                If (solarYear Mod 4 = 0 And solarYear Mod 100 <> 0) Or solarYear Mod 400 = 0 Then
                    SolarDaysInMonth = 29
                Else
                    SolarDaysInMonth = 28
                End If
        End Select
    End Function


    Hiện tại em chỉ sử dụng chatGPT cho từng phần code rồi ráp lại thì nó nhanh. Nếu hỏi nguyên bộ code chuyển lịch Âm Dương như ví dụ của anh thì nó sẽ tìm kiếm trong kho dữ liệu có cái nào tương đương là nó đẩy lên thôi, không tin tưởng hoàn toàn được, chờ nó học thêm nữa.

    Code đang chạy ngon là của Hồ Ngọc Đức.

    Mã PHP:
    Option Explicit
    Const PI As Double 3.14159265358979 ' Atn(1) * 4

    Compute the (integralJulian day number of day dd/mm/yyyyi.e., the number
    ' of days between 1/1/4713 BC (Julian calendar) and dd/mm/yyyy.
    Formula from http://www.tondering.dk/claus/calendar.html
    Function jdFromDate(ByVal dd As LongByVal mm As LongByVal yy As Long) As Long
        Dim a 
    As DoubleAs LongAs Longjd As Long
        a 
    Fix((14 mm) / 12)
        y yy 4800 a
        m 
    mm 12 3
        jd 
    dd Fix((153 2) / 5) + 365 y _
            
    Fix(4) - Fix(100) + Fix(400) - 32045
        
    If jd 2299161 Then
            jd 
    dd Fix((153 2) / 5) + 365 Fix(4) - 32083
        End 
    If
        jdFromDate jd
    End 
    Function

    ' Convert a Julian day number to day/month/year. Parameter jd is an integer
    Function jdToDate(ByVal jd As Long)
        Dim a As Long, b As Long, c As Long, d As Long, e As Long, m As Long
        Dim Day As Long, Month As Long, Year As Long
        If (jd > 2299160) Then ' 
    After 5/10/1582Gregorian calendar
            a 
    jd 32044
            b 
    Fix((3) / 146097)
            c Fix((146097) / 4)
        Else
            b 0
            c 
    jd 32082
        End 
    If
        d Fix((3) / 1461)
        e Fix((1461 d) / 4)
        m Fix((2) / 153)
        Day Fix((153 2) / 5) + 1
        Month 
    12 Fix(10)
        Year 100 4800 Fix(10)
        jdToDate = Array(DayMonthYear)
    End Function

    ' Compute the time of the k-th new moon after the new moon of 1/1/1900 13:52 UCT
    (measured as the number of days since 1/1/4713 BC noon UCT,
    ' e.g., 2451545.125 is 1/1/2000 15:00 UTC).
    Returns a floating numbere.g.,
    ' 2415079.9758617813 for k=2 or 2414961.935157746 for k=-2

    Function NewMoon(ByVal k As Long) As Double
        Dim T As Double, T2 As Double, T3 As Double, dr As Double
        Dim Jd1 As Double, m As Double, Mpr As Double
        Dim F As Double, C1 As Double, deltat As Double, JdNew As Double
        T = k / 1236.85 ' 
    Time in Julian centuries from 1900 January 0.5
        T2 
    T
        T3 
    T2 T
        dr 
    PI 180
        Jd1 
    2415020.75933 29.53058868 0.0001178 T2 0.000000155 T3
        Jd1 
    Jd1 0.00033 Sin((166.56 132.87 0.009173 T2) * dr)
            ' Mean new moon
        m = 359.2242 + 29.10535608 * k - 0.0000333 * T2 - 0.00000347 * T3
            ' 
    Sun's mean anomaly
        Mpr = 306.0253 + 385.81691806 * k + 0.0107306 * T2 + 0.00001236 * T3
            ' 
    Moon's mean anomaly
        F = 21.2964 + 390.67050646 * k - 0.0016528 * T2 - 0.00000239 * T3
            ' 
    Moon's argument of latitude
        C1 = (0.1734 - 0.000393 * T) * Sin(m * dr) + 0.0021 * Sin(2 * dr * m)
        C1 = C1 - 0.4068 * Sin(Mpr * dr) + 0.0161 * Sin(dr * 2 * Mpr)
        C1 = C1 - 0.0004 * Sin(dr * 3 * Mpr)
        C1 = C1 + 0.0104 * Sin(dr * 2 * F) - 0.0051 * Sin(dr * (m + Mpr))
        C1 = C1 - 0.0074 * Sin(dr * (m - Mpr)) + 0.0004 * Sin(dr * (2 * F + m))
        C1 = C1 - 0.0004 * Sin(dr * (2 * F - m)) - 0.0006 * Sin(dr * (2 * F + Mpr))
        C1 = C1 + 0.001 * Sin(dr * (2 * F - Mpr)) + 0.0005 * Sin(dr * (2 * Mpr + m))
        If (T < -11) Then
            deltat = 0.001 + 0.000839 * T + 0.0002261 * T2 _
                    - 0.00000845 * T3 - 0.000000081 * T * T3
        Else
            deltat = -0.000278 + 0.000265 * T + 0.000262 * T2
        End If
        JdNew = Jd1 + C1 - deltat
        NewMoon = JdNew
    End Function

    Compute the longitude of the sun at any time.
    ' Parameter: floating number jdn, the number of days since 1/1/4713 BC noon

    Function SunLongitude(ByVal jdn As Double) As Double
        Dim T As Double, T2 As Double, dr As Double, m As Double
        Dim L0 As Double, DL As Double, L As Double
        T = (jdn - 2451545) / 36525
            ' 
    Time in Julian centuries from 2000-01-01 12:00:00 GMT
        T2 
    T
        dr 
    PI 180 ' degree to radian
        m = 357.5291 + 35999.0503 * T - 0.0001559 * T2 - 0.00000048 * T * T2
            ' 
    mean anomalydegree
        L0 
    280.46645 36000.76983 0.0003032 T2
            
    ' mean longitude, degree
        DL = (1.9146 - 0.004817 * T - 0.000014 * T2) * Sin(dr * m)
        DL = DL + (0.019993 - 0.000101 * T) * Sin(dr * 2 * m) _
            + 0.00029 * Sin(dr * 3 * m)
        L = L0 + DL ' 
    true longitudedegree
        L 
    dr
        L 
    PI * (Fix(/ (PI 2))) ' Normalize to (0, 2*PI)
        SunLongitude = L
    End Function

    Compute sun position at midnight of the day with the given Julian day number.
    ' The time zone if the time difference between local time and UTC: 7.0 for UTC+7:00.
    The function returns a number between 0 and 11.
    ' From the day after March equinox and the 1st major term after March equinox,
    0 is returnedAfter that, return 12...
    Function 
    getSunLongitude(ByVal dayNumber As DoubleByVal timeZone As Byte) As Long
        getSunLongitude 
    Fix(SunLongitude(dayNumber 0.5 timeZone 24) / PI 6)
    End Function

    ' Compute the day of the k-th new moon in the given time zone.
    The time zone if the time difference between local time and UTC7.0 for UTC+7:00
    Function getNewMoonDay(ByVal k As LongByVal timeZone As Long) As Long
        getNewMoonDay 
    Fix(NewMoon(k) + 0.5 timeZone 24)
    End Function

    ' Find the day that starts the luner month 11 of the given year
    for the given time zone
    Function getLunarMonth11(ByVal yy As LongByVal timeZone As Long) As Long
        Dim k 
    As Longoff As Doublenm As LongsunLong As Double
        
    '' off jdFromDate(3112yy) - 2415021.076998695
        off 
    jdFromDate(3112yy) - 2415021
        k 
    Fix(off 29.530588853)
        nm getNewMoonDay(ktimeZone)
        sunLong getSunLongitude(nmtimeZone' sun longitude at local midnight
        If (sunLong >= 9) Then
            nm = getNewMoonDay(k - 1, timeZone)
        End If
        getLunarMonth11 = nm
    End Function

    Find the index of the leap month after the month starting on the day a11.
    Function 
    getLeapMonthOffset(ByVal a11 As DoubleByVal timeZone As Long) As Long
        Dim k 
    As Longlast As LongArc As LongAs Long
        k 
    Fix((a11 2415021.07699869) / 29.530588853 0.5)
        last 0
        I 
    ' We start with the month following lunar month 11
        Arc = getSunLongitude(getNewMoonDay(k + I, timeZone), timeZone)
        Do
            last = Arc
            I = I + 1
            Arc = getSunLongitude(getNewMoonDay(k + I, timeZone), timeZone)
        Loop While (Arc <> last And I < 14)
        getLeapMonthOffset = I - 1
    End Function

    Comvert solar date dd/mm/yyyy to the corresponding lunar date
    Function Solar2Lunar_
            ByVal dd 
    As Long_
            ByVal mm 
    As Long_
            Optional ByVal yy 
    As Long 0_
            Optional ByVal timeZone 
    As Long 7) As String
        Dim k 
    As Longdiff As LongleapMonthDiff As LongdayNumber As Long
        Dim monthStart 
    As Doublea11 As Longb11 As Long
        Dim lunarDay 
    As DoublelunarMonth As LonglunarYear As LonglunarLeap As Long
        
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        If yy = 0 Then yy = Year(Date)
        dayNumber = jdFromDate(dd, mm, yy)
        k = Fix((dayNumber - 2415021.07699869) / 29.530588853)
        monthStart = getNewMoonDay(k + 1, timeZone)
        If (monthStart > dayNumber) Then
            monthStart = getNewMoonDay(k, timeZone)
        End If
        ' 
    alert(dayNumber " -> " monthStart)
        a11 getLunarMonth11(yytimeZone)
        b11 a11
        
    If (a11 >= monthStartThen
            lunarYear 
    yy
            a11 
    getLunarMonth11(yy 1timeZone)
        Else
            lunarYear yy 1
            b11 
    getLunarMonth11(yy 1timeZone)
        End If
        lunarDay dayNumber monthStart 1
        diff 
    Fix((monthStart a11) / 29)
        lunarLeap 0
        lunarMonth 
    diff 11
        
    If (b11 a11 365Then
            leapMonthDiff 
    getLeapMonthOffset(a11timeZone)
            If (diff >= leapMonthDiffThen
                lunarMonth 
    diff 10
                
    If (diff leapMonthDiffThen lunarLeap 1
            End 
    If
        End If
        If (lunarMonth 12Then lunarMonth lunarMonth 12
        
    If (lunarMonth >= 11 And diff 4Then lunarYear lunarYear 1
        Solar2Lunar 
    Format(lunarDay"00") & _
                    
    "/" Format(lunarMonth"00") & _
                    
    "/" Format(lunarYear"0000 \A\L") & IIf(lunarLeap" (" lunarMonth " N)""")
    End Function

    ' Convert a lunar date to the corresponding solar date
    Function Lunar2Solar( _
            ByVal lunarDay As Long, _
            ByVal lunarMonth As Long, _
            Optional ByVal lunarYear As Long = 0, _
            Optional ByVal lunarLeap As Long = 0, _
            Optional ByVal timeZone As Long = 7) As Date
        Dim k As Long, a11 As Long, b11 As Long, off As Long, leapOff As Long
        Dim LeapMonth As Long, monthStart As Long
        '
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        If lunarYear 0 Then lunarYear Year(Date)
        If (lunarMonth 11Then
            a11 
    getLunarMonth11(lunarYear 1timeZone)
            b11 getLunarMonth11(lunarYeartimeZone)
        Else
            a11 getLunarMonth11(lunarYeartimeZone)
            b11 getLunarMonth11(lunarYear 1timeZone)
        End If
        k Fix(0.5 + (a11 2415021.07699869) / 29.530588853)
        off lunarMonth 11
        
    If (off 0Then off off 12
        
    If (b11 a11 365Then
            leapOff 
    getLeapMonthOffset(a11timeZone)
            LeapMonth leapOff 2
            
    If (LeapMonth 0Then LeapMonth LeapMonth 12
            
    If (lunarLeap <> And lunarMonth <> LeapMonthThen
                Lunar2Solar 
    = Array(000)
                Exit Function
            ElseIf (lunarLeap <> Or off >= leapOffThen
                off 
    off 1
            End 
    If
        End If
        monthStart getNewMoonDay(offtimeZone)
        Dim R
        R 
    jdToDate(monthStart lunarDay 1)
        Lunar2Solar DateSerial(R(2), R(1), R(0))
    End Function 
  • RE: Nhờ kiểm tra code

    tranthanhan1962 > 21-03-23, 11:32 PM

    (21-03-23, 10:43 PM)ongke0711 Đã viết: Code này nó cho kết quả sai và hiển thị cũng lung tung luôn.
    Em hỏi bằng tiếng Anh thì nó cũng ra code nhưng cũng sai tè le.
    Thank nhiều. Hôm nay, mới cài lại máy làm tới Chat GPT nên test nó một cái, còn cài một số phần mềm nên nhờ anh em kiểm tra hộ. Vả lại mình cũng không tin tưởng vào cái AI này nên đưa lên dể mọi người thấy nó cũng tào lao. Ngay cả 2+2 =4 hay =3 nó còn chưa dám khẳng định.
    Kiểu như: Tôi hỏi Chat GPT.
    Tôi: 2+2 =?
    Chat GPT: 2+2=4
    Tôi: Nhưng vợ tôi nói 2+2=3 và Chat GPT đã sai.
    Chat GPT: Có thể vợ bạn đúng và Chat GPT sai.
    Một bài toán đơn giản như vậy mà Chat GPT còn không xác định chinh xác thì nó làm được cái gì ra hồn. Có điều trong tương lai có thể nó làm tốt hơn. Kiểu như Deep Blue đánh cò với Casparov. Lần thứ 1 Casparov thắng dễ dàng, lần thứ 2 Casparov đánh bậy (kiểu chưa từng có 1 kiện tướng nào đánh như vậy) mới thắng, lần thứ 3 Casparov đánh kiểu gì cũng thua, lần thứ 4 Casparov từ chối thi đấu với Deep Blue (chạy mặc luôn).  034
  • RE: Nhờ kiểm tra code

    ongke0711 > 22-03-23, 12:18 AM

    chatGPT nó hơn Google ở chỗ nó lấy dữ liệu từ kho dữ liệu được cung cấp rồi tổng hợp lại đúng như những gì mình hỏi, còn google thì mình phải tự đọc các bài viết để ra kết quả. Nguồn dữ liệu của nó chủ yếu từ Stack Overflow, Quora, Reddit...Những ai đã trả lời qua thì nó sẽ lấy đó làm kết quả cho những người hỏi khác. Do đó nếu nguồn dữ liệu đưa vào chính xác nhiều lần thì nó lấy dữ liệu đó trả lời cho mình.
    Em thấy nó tiện ở chỗ, mình hỏi các bộ code nhỏ thì nó tổng hợp khá chính xác, mình copy paste, chỉnh sửa lại chút nếu nó sai gì đó rồi xong, rất nhanh, khỏi phải viết code toàn bộ, mất thời gian hơn.
    Ví dụ:

    [Hình: w2nrv3Nh.png]