当前位置:网站首页>golang日期时间time包代码示例: 根据生日获取年龄、生肖、星座
golang日期时间time包代码示例: 根据生日获取年龄、生肖、星座
2022-06-23 15:38:00 【学亮编程手记】
package main
import (
"fmt"
"time"
)
func GetTimeFromStrDate(date string) (year, month, day int) {
const shortForm = "2006-01-02"
d, err := time.Parse(shortForm, date)
if err != nil {
fmt.Println("出生日期解析错误!")
return 0, 0, 0
}
year = d.Year()
month = int(d.Month())
day = d.Day()
return
}
func GetZodiac(year int) (zodiac string) {
if year <= 0 {
zodiac = "-1"
}
start := 1901
x := (start - year) % 12
if x == 1 || x == -11 {
zodiac = "鼠"
}
if x == 0 {
zodiac = "牛"
}
if x == 11 || x == -1 {
zodiac = "虎"
}
if x == 10 || x == -2 {
zodiac = "兔"
}
if x == 9 || x == -3 {
zodiac = "龙"
}
if x == 8 || x == -4 {
zodiac = "蛇"
}
if x == 7 || x == -5 {
zodiac = "马"
}
if x == 6 || x == -6 {
zodiac = "羊"
}
if x == 5 || x == -7 {
zodiac = "猴"
}
if x == 4 || x == -8 {
zodiac = "鸡"
}
if x == 3 || x == -9 {
zodiac = "狗"
}
if x == 2 || x == -10 {
zodiac = "猪"
}
return
}
func GetAge(year int) (age int) {
if year <= 0 {
age = -1
}
nowyear := time.Now().Year()
age = nowyear - year
return
}
func GetConstellation(month, day int) (star string) {
if month <= 0 || month >= 13 {
star = "-1"
}
if day <= 0 || day >= 32 {
star = "-1"
}
if (month == 1 && day >= 20) || (month == 2 && day <= 18) {
star = "水瓶座"
}
if (month == 2 && day >= 19) || (month == 3 && day <= 20) {
star = "双鱼座"
}
if (month == 3 && day >= 21) || (month == 4 && day <= 19) {
star = "白羊座"
}
if (month == 4 && day >= 20) || (month == 5 && day <= 20) {
star = "金牛座"
}
if (month == 5 && day >= 21) || (month == 6 && day <= 21) {
star = "双子座"
}
if (month == 6 && day >= 22) || (month == 7 && day <= 22) {
star = "巨蟹座"
}
if (month == 7 && day >= 23) || (month == 8 && day <= 22) {
star = "狮子座"
}
if (month == 8 && day >= 23) || (month == 9 && day <= 22) {
star = "处女座"
}
if (month == 9 && day >= 23) || (month == 10 && day <= 22) {
star = "天秤座"
}
if (month == 10 && day >= 23) || (month == 11 && day <= 21) {
star = "天蝎座"
}
if (month == 11 && day >= 22) || (month == 12 && day <= 21) {
star = "射手座"
}
if (month == 12 && day >= 22) || (month == 1 && day <= 19) {
star = "魔蝎座"
}
return star
}
func main() {
y, m, d := GetTimeFromStrDate("1986-06-18")
fmt.Println(GetAge(y)) // 获取年龄
fmt.Println(GetConstellation(m, d)) // 星座
fmt.Println(GetZodiac(y)) // 生肖
}
边栏推荐
- 阻塞、非阻塞、多路复用、同步、异步、BIO、NIO、AIO 一文搞定
- CA认证和颁发吊销证书
- 【TcaplusDB知识库】Tmonitor单机安装指引介绍(一)
- [tcapulusdb knowledge base] Introduction to tmonitor stand-alone installation guidelines (II)
- 企业想上MES系统?还得满足这些条件
- OpenResty 基础
- HBuilderX-Light 主题能不能加个批注功能?
- Sleuth + Zipkin
- Summarize the experience of purchasing Alibaba cloud servers
- 机器人方向与高考选专业的一些误区
猜你喜欢
随机推荐
子级文件拖到上一级
R语言使用tidyquant包的tq_transmute函数计算持有某只股票的天、月、周收益率、ggplot2使用条形图(bar plot)可视化股票月收益率数据、使用百分比显示Y轴坐标数据
Spin lock using CAS
stylegan1: a style-based henerator architecture for gemerative adversarial networks
PageHelper faces the paging problem of complex service data processing
Advanced development - generic entry basic class test
中大人脸素描FERET数据库(CUFSF)
Object
js 递归json树 根据 子id 查 父id
Block, non block, multiplexing, synchronous, asynchronous, bio, NiO, AIO
如何让销售管理更高效?
[tcapulusdb knowledge base] Introduction to tmonitor stand-alone installation guidelines (I)
图片保存:torchvision.utils.save_image(img, imgPath)
MySQL transactions and locks
阻塞、非阻塞、多路复用、同步、异步、BIO、NIO、AIO 一文搞定
ABP framework - data access infrastructure (Part 2)
npm 如何发包 删包
[tcapulusdb knowledge base] Introduction to tmonitor background one click installation (I)
openGauss数据库源码解析系列文章—— 密态等值查询技术详解(上)
Build vscode into an invincible IDE (14) tasks JSON and launch JSON configuration details, add automation tasks at will








