当前位置:网站首页>Code example of golang date time package: get age, zodiac and constellation based on birthday
Code example of golang date time package: get age, zodiac and constellation based on birthday
2022-06-23 16:23:00 【Learn programming notes】
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(" Birth date parsing error !")
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 = " rat "
}
if x == 0 {
zodiac = " cattle "
}
if x == 11 || x == -1 {
zodiac = " The tiger "
}
if x == 10 || x == -2 {
zodiac = " rabbit "
}
if x == 9 || x == -3 {
zodiac = " dragon "
}
if x == 8 || x == -4 {
zodiac = " The snake "
}
if x == 7 || x == -5 {
zodiac = " Horse "
}
if x == 6 || x == -6 {
zodiac = " sheep "
}
if x == 5 || x == -7 {
zodiac = " Monkey "
}
if x == 4 || x == -8 {
zodiac = " chicken "
}
if x == 3 || x == -9 {
zodiac = " Dog "
}
if x == 2 || x == -10 {
zodiac = " The pig "
}
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 = " Aquarius: "
}
if (month == 2 && day >= 19) || (month == 3 && day <= 20) {
star = " Pisces "
}
if (month == 3 && day >= 21) || (month == 4 && day <= 19) {
star = " Aries "
}
if (month == 4 && day >= 20) || (month == 5 && day <= 20) {
star = " Taurus "
}
if (month == 5 && day >= 21) || (month == 6 && day <= 21) {
star = " Gemini "
}
if (month == 6 && day >= 22) || (month == 7 && day <= 22) {
star = " Cancer "
}
if (month == 7 && day >= 23) || (month == 8 && day <= 22) {
star = " Leo "
}
if (month == 8 && day >= 23) || (month == 9 && day <= 22) {
star = " Virgo "
}
if (month == 9 && day >= 23) || (month == 10 && day <= 22) {
star = " libra "
}
if (month == 10 && day >= 23) || (month == 11 && day <= 21) {
star = " scorpio "
}
if (month == 11 && day >= 22) || (month == 12 && day <= 21) {
star = " Sagittarius "
}
if (month == 12 && day >= 22) || (month == 1 && day <= 19) {
star = " Scorpio "
}
return star
}
func main() {
y, m, d := GetTimeFromStrDate("1986-06-18")
fmt.Println(GetAge(y)) // For age
fmt.Println(GetConstellation(m, d)) // The constellation
fmt.Println(GetZodiac(y)) // the Chinese zodiac
}
边栏推荐
- Does the enterprise want to use the MES system? These conditions have to be met
- 子级文件拖到上一级
- Medical image segmentation website
- Web篇_01 了解web開發
- After the model is created, initialize the variables in con2d, convtranspose2d, and normalized batchnorm2d functions
- js 对象 使用小技巧
- Understand the classic buck-boost negative voltage circuit
- 线程池
- Block, non block, multiplexing, synchronous, asynchronous, bio, NiO, AIO
- 解读2022年度敏捷教练行业现状报告
猜你喜欢

A tour of grpc:01 - Basic Theory

golang二分查找法代码实现
![[tcapulusdb knowledge base] Introduction to tmonitor stand-alone installation guidelines (I)](/img/7b/8c4f1549054ee8c0184495d9e8e378.png)
[tcapulusdb knowledge base] Introduction to tmonitor stand-alone installation guidelines (I)

SaaS cloud tool, a sharp tool for change under the industrial Internet

Ten thousand words introduction, detailed explanation of the core technical points of Tencent interview (t1-t9), and arrangement of interview questions

腾讯的技术牛人们,是如何完成全面上云这件事儿的?

Golang对JSON文件的写操作

融云:让银行轻松上“云”

2022九峰小学(光谷第二十一小学)生源摸底

Uniapp sends picture messages to Tencent instant messaging Tim
随机推荐
Web容器是怎样给第三方插件做初始化工作的
CoAtNet: Marrying Convolution and Attention for All Data Sizes翻译
【TcaplusDB知识库】Tmonitor单机安装指引介绍(一)
MATLAB中iscellstr函数的使用
js 对象 使用小技巧
Quartz
Block, non block, multiplexing, synchronous, asynchronous, bio, NiO, AIO
Object
golang写文件代码示例
Generating binary search balanced tree [using tree recursion]
Object
Array's own method
npm install 问题解决(nvm安装与使用)
openGauss数据库源码解析系列文章—— 密态等值查询技术详解(下)
多年亿级流量下的高并发经验总结,都毫无保留地写在了这本书中
window远程桌面连接互传文件加速小技巧
六石编程学:运用之妙,存乎一心
Image saving: torchvision utils. save_ image(img, imgPath)
OutputDebugString使用说明以及异常处理
TCP protocol notes