123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- // Package utils @Author gmy 2024/8/6 16:06:00
- package utils
- import (
- "fmt"
- "strings"
- "time"
- )
- // ParseDateAndWeek parseDateAndWeek 解析日期并计算当前周数 ==> 24年31周
- func ParseDateAndWeek(dateText string) (string, error) {
- // 解析日期
- reportDate, err := time.Parse("2006-01-02", strings.TrimSpace(strings.Split(dateText, " ")[0]))
- if err != nil {
- return "", fmt.Errorf("failed to parse report date: %v", err)
- }
- // 计算年份和周数
- year, week := reportDate.ISOWeek()
- // 获取年份的后两位
- shortYear := year % 100
- targetWeek := fmt.Sprintf("%02d年第%d周", shortYear, week)
- return targetWeek, nil
- }
- // ParseDateAndMonth 解析时间并计算当前月份 和 后两月 1月就是1月F,二月是二月G 规则:F=1月,G=2月,H=3月,J=4月,K=5月,M=6月,N=7月,Q=8月,U=9月,V=10月,X=11月,Z=12月
- func ParseDateAndMonth(dateText string) ([]string, error) {
- reportDate, err := time.Parse("2006-01-02", strings.TrimSpace(strings.Split(dateText, " ")[0]))
- if err != nil {
- return nil, fmt.Errorf("failed to parse report date: %v", err)
- }
- months := make([]string, 3)
- monthMap := map[string]string{
- "01": "1月F",
- "02": "2月G",
- "03": "3月H",
- "04": "4月J",
- "05": "5月K",
- "06": "6月M",
- "07": "7月N",
- "08": "8月Q",
- "09": "9月X",
- "10": "10月X",
- "11": "11月X",
- "12": "12月Z",
- }
- for i := 0; i < 3; i++ {
- month := reportDate.AddDate(0, i, 0).Format("01")
- months[i] = monthMap[month]
- }
- return months, nil
- }
- // GetCurrentTime 获取当前时间 格式为 2024-08-07 15:29:58
- func GetCurrentTime() string {
- return time.Now().Format("2006-01-02 15:04:05")
- }
- // ConvertTimeFormat 转换时间格式 dateText 格式为 2024-08-03 07:53 --> 2024-08-03
- func ConvertTimeFormat(dateText string) (string, error) {
- // 解析日期
- reportDate, err := time.Parse("2006-01-02 15:04", strings.TrimSpace(dateText))
- if err != nil {
- return "", fmt.Errorf("failed to parse report date: %v", err)
- }
- return reportDate.Format("2006-01-02"), nil
- }
- // ConvertTimeFormatToYearMonth 转换时间格式 dateText 返回本月 和 后两月 格式为 2024-08-03 --> 2024年8月,2024-10-03 --> 2024年10月
- func ConvertTimeFormatToYearMonth(dateText string) ([]string, error) {
- // 解析日期
- reportDate, err := time.Parse("2006-01-02", strings.TrimSpace(dateText))
- if err != nil {
- return nil, fmt.Errorf("failed to parse report date: %v", err)
- }
- months := make([]string, 3)
- for i := 0; i < 3; i++ {
- month := reportDate.AddDate(0, i, 0).Format("2006年1月")
- months[i] = month
- }
- return months, nil
- }
- // GetCurrentYearAndNextYear 获取当时所在得年度和明年得年度列表 2024-08-03 --> 2023/24年度, 2024/25年度
- func GetCurrentYearAndNextYear(dateText string) ([]string, error) {
- // 解析日期
- reportDate, err := time.Parse("2006-01-02", strings.TrimSpace(dateText))
- if err != nil {
- return nil, fmt.Errorf("failed to parse report date: %v", err)
- }
- years := make([]string, 2)
- year := reportDate.Year()
- // 当前年度
- years[0] = fmt.Sprintf("%d/%02d年度", year-1, year%100)
- // 下一年度
- years[1] = fmt.Sprintf("%d/%02d年度", year, (year+1)%100)
- return years, nil
- }
- // GetCurrentMonth 获取当前月份 2024-08-03 --> 8月
- func GetCurrentMonth(dateText string) (string, error) {
- // 解析日期
- reportDate, err := time.Parse("2006-01-02", strings.TrimSpace(dateText))
- if err != nil {
- return "", fmt.Errorf("failed to parse report date: %v", err)
- }
- // 计算月份
- month := reportDate.Month()
- return fmt.Sprintf("%d月", month), nil
- }
|