Quick actions

cmd+k|ctrl+k

Navigation

Languages

Durasi pertanggal

Snippet info

Language

Go

Visibility

public

Author

riki.whyudi

Created

2023-01-23T01:25:48.054529Z

Updated

2023-01-23T01:25:48.054529Z

package main

import (
    "fmt"
    "time"
)

func getNextMonth(now time.Time) time.Time {
    var nextMonth time.Time
    endMonth := (now.Month() + 1) % 12
    if endMonth == 2 { // Februari
        if now.Year()%4 == 0 { // kabisat
            nextMonth = time.Date(now.Year(), time.February, 29, now.Hour(), now.Minute(), now.Second(), now.Nanosecond(), now.Location())
        } else {
            nextMonth = time.Date(now.Year(), time.February, 28, now.Hour(), now.Minute(), now.Second(), now.Nanosecond(), now.Location())
        }
    } else if endMonth == 4 || endMonth == 6 || endMonth == 9 || endMonth == 11 {
        nextMonth = time.Date(now.Year(), time.Month(endMonth), 30, now.Hour(), now.Minute(), now.Second(), now.Nanosecond(), now.Location())
    } else {
        nextMonth = time.Date(now.Year(), time.Month(endMonth), 31, now.Hour(), now.Minute(), now.Second(), now.Nanosecond(), now.Location())
    }
    return nextMonth
}

func main() {
    now := time.Now()

    // Tampilkan tanggal, bulan, dan tahun sekarang
    fmt.Println("Tanggal sekarang: ", now.Day())
    fmt.Println("Bulan sekarang: ", int(now.Month()))
    fmt.Println("Tahun sekarang: ", now.Year())
    fmt.Println("Waktu sekarang: ", now.Format("15:04:05"))

    // tambahkan satu bulan ke waktu sekarang
    nextMonth := getNextMonth(now)

    // Tampilkan tanggal, bulan, dan tahun satu bulan kemudian
    fmt.Println("Tanggal satu bulan kemudian: ", nextMonth.Day())
    fmt.Println("Bulan satu bulan kemudian: ", int(nextMonth.Month()))
    fmt.Println("Tahun satu bulan kemudian: ", nextMonth.Year())
    fmt.Println("Waktu satu bulan kemudian: ", nextMonth.Format("15:04:05"))
}
INFO