当前位置:网站首页>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)) // 生肖
}
边栏推荐
- 【OpenHarmony】usb gadget 配置hdc功能cfg文件解读
- R语言ggplot2可视化水平箱图(Horizontal boxplot with coord_flip)、并添加抖动数据点显示分布情况(jittered points)
- Detailed explanation of MQ message oriented middleware theory
- js 递归json树 根据 子id 查 父id
- 港股多支个股表现活跃,引发投资者对港股市场回暖猜想与关注
- R语言使用timeROC包计算无竞争情况下的生存资料多时间AUC值、使用cox模型、并添加协变量、可视化无竞争情况下的生存资料多时间ROC曲线
- Log4J日志整合及配置详解
- R语言使用tidyquant包的tq_transmute函数计算持有某只股票的天、月、周收益率、ggplot2使用条形图(bar plot)可视化股票月收益率数据、使用百分比显示Y轴坐标数据
- Tips for accelerating file transfer between windows remote desktop connections
- Example of if directly judging data type in JS
猜你喜欢

【TcaplusDB知识库】Tmonitor单机安装指引介绍(一)

Tips for accelerating file transfer between windows remote desktop connections

XML

【TcaplusDB知识库】Tmonitor后台一键安装介绍(一)

英特尔Arc A380显卡消息汇总:跑分亮眼驱动拉胯 入门性价产品亟待优化

【TcaplusDB知识库】Tmonitor后台一键安装介绍(二)

总结一下购买阿里云服务器的经验

npm install 问题解决(nvm安装与使用)

SaaS 云工具,产业互联网下的变革利器
![[tcapulusdb knowledge base] Introduction to tmonitor stand-alone installation guidelines (II)](/img/6d/8b1ac734cd95fb29e576aa3eee1b33.png)
[tcapulusdb knowledge base] Introduction to tmonitor stand-alone installation guidelines (II)
随机推荐
Ten thousand words introduction, detailed explanation of the core technical points of Tencent interview (t1-t9), and arrangement of interview questions
解读2022年度敏捷教练行业现状报告
SSRS页面配置Postgresql data source的方法
Object
ADB key name, key code number and key description comparison table
golang冒泡排序代码实现
[tcapulusdb knowledge base] Introduction to tmonitor system upgrade
OutputDebugString使用说明以及异常处理
Charge pump principle handout, how is the voltage "pumped"?
腾讯的技术牛人们,是如何完成全面上云这件事儿的?
【TcaplusDB知识库】TcaplusDB Tmonitor模块架构介绍
医学影像分割的网站
数组自带的方法
139. word splitting
企业想上MES系统?还得满足这些条件
图片保存:torchvision.utils.save_image(img, imgPath)
将vscode打造无敌的IDE(14) tasks.json和launch.json配置详解,随心所欲添加自动化任务
规避这六大难点,成功实施MES系统
任何代码未动的情况下第二天项目访问速度明显下降,案例分析
[openharmony] USB gadget configuration HDC function cfg file interpretation