/golang 字符串截取/
//截取字符串 start 起点下标 length 需要截取的长度
func Substr(str string, start int, length int) string {
rs := []rune(str)
rl := len(rs)
end := 0
if start < 0 {
start = rl - 1 + start
}
end = start + length
if start > end {
start, end = end, start
}
if start < 0 {
start = 0
}
if start > rl {
start = rl
}
if end < 0 {
end = 0
}
if end > rl {
end = rl
}
return string(rs[start:end])
}
//截取字符串 start 起点下标 end 终点下标(不包括)
func Substr2(str string, start int, end int) string {
rs := []rune(str)
length := len(rs)
if start < 0 || start > length {
panic("start is wrong")
}
if end < 0 || end > length {
panic("end is wrong")
}
return string(rs[start:end])
}
func StrDate()(string) {
var date string
year:=Substr(time.Now().Format("2006-01-02 15:04"),0,4)
month:= Substr(time.Now().Format("2006-01-02 15:04"),5,2)
year_num,_:=strconv.Atoi(year)
month_num,_:=strconv.Atoi(month)
if(month_num>8||month_num<2){
year_num++
date= year+"-"+strconv.Itoa(year_num)+"-1"
}else if(month_num>=2&&month_num<=8){
year_num--
date=strconv.Itoa(year_num)+"-"+year+"-2"
}
return date
}
func StrDateLast()(string) {
var date string
year:=Substr(time.Now().Format("2006-01-02 15:04"),0,4)
month:= Substr(time.Now().Format("2006-01-02 15:04"),5,2)
year_num,_:=strconv.Atoi(year)
month_num,_:=strconv.Atoi(month)
if(month_num>8||month_num<2){
year_num--
date=strconv.Itoa(year_num)+"-"+year+"-2"
}else if(month_num>=2&&month_num<=8){
year_num--
date=strconv.Itoa(year_num)+"-"+year+"-1"
}
return date
}
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于