日期格式 日期运算 mysql 时间 时间戳 字符串转time.struct_timed 转datetime 时间差计算 取出日期所在的周的周一的日期
时间消耗 耗时
import time
from datetime import datetime
limit_ms = 10000
start = datetime.now()
time.sleep(1.234)
end = datetime.now()
duration = end - start
ms = duration.total_seconds()
slow = duration.days >= 0 or ms > limit_ms
mysql 时间差
TIMESTAMPDIFF(SECOND,end_time,NOW()) AS diff
TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2)
其中,unit
表示需要返回差异的时间单位。可以使用以下单位:
MICROSECOND
SECOND
MINUTE
HOUR
DAY
WEEK
MONTH
QUARTER
YEAR
datetime_expr1
和datetime_expr2
是两个日期时间值,datetime_expr1
在datetime_expr2
之前或之后。可以使用以下函数来获取日期时间值:
NOW()
: 返回当前日期时间CURDATE()
: 返回当前日期CURTIME()
: 返回当前时间
计算起止日期的日期清单
from datetime import datetime, timedelta
start_date, end_date = '2024-06-23', '2024-07-04',
fmt = '%Y-%m-%d'
s = datetime.strptime(start_date, fmt)
e = datetime.strptime(end_date, fmt)
ret = [start_date]
for i in range(1, (e - s).days, 1):
t = s + timedelta(days=i)
ret.append(t.strftime(fmt))
ret.append(end_date)
列表中词典排序
'date_list': [{t[0]: t[1]} for t in sorted(ret1.items(), key=lambda i: i[0])
[
'2024-06-18':{},
'2024-05-18':{},
'2024-09-18':{}
]
select current_timestamp, CURRENT_TIMESTAMP(), date_sub(now(), interval 1 HOUR), date_sub(now(), interval 1 HOUR)>"2024-03-06 11:25:17", date_sub(now(), interval 1 HOUR)>"2024-03-06 10:25:17";
Java 实例 – 时间戳转换成时间 | 菜鸟教程 https://www.runoob.com/java/date-timestamp2date.html
时间转时间戳
import java.text.SimpleDateFormat;
import java.util.Date;
public static String dateToStamp(String s) throws ParseException {
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = simpleDateFormat.parse(s);
long ts = date.getTime();
System.out.println(ts);
System.out.println(ts/1000);
res = String.valueOf(ts/1000);
return res;
}
String dTStr="2023-10-18 09:48:00";
try {
dTStr = Util.dateToStamp(dTStr);
dT = Util.getDouble(dTStr);
System.out.println(dT);
} catch (Exception e) {
e.printStackTrace();
System.out.println("输入有误!");
}
1697593680000
1697593680
1.69759368E9
当前时间
import java.util.Date;
java.util.Date day=new Date();
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(day));
MySQL 时间函数加减计算_mysql小时加减_云中鲸的博客-CSDN博客 https://blog.csdn.net/qq_38486203/article/details/80654040
SELECT CURRENT_TIME(),NOW(), DATE_ADD(NOW(), INTERVAL 3 HOUR), DATE_ADD(NOW(), INTERVAL 1 DAY), DATE_ADD(NOW(), INTERVAL -1 DAY);
SELECT a.*,YEAR(a.create_dt),Month(a.create_dt),Day(a.create_dt),Hour(a.create_dt),DATE_FORMAT(create_dt, '%Y%c%d') FROM mqtt_origin_data a
mysql 当期时间
CURRENT_TIMESTAMP 2023-05-31 11:59:38
js 零时时间戳
当日
当前时间戳
const nowTs= parseInt((new Date().getTime() / 1000).toString());
今天零时时间戳
const t0=nowTs- (nowTs% 86400) - 3600 * 8
时间戳区间
近n天,n=1,2,...,其中,n=1为今天
[t0-(n-1)*24*3600,t0)
取整运算
Go-获取当天、昨天0点时间戳以及格式化时间_luyaran的博客-CSDN博客_go 获取当天0点 https://blog.csdn.net/luyaran/article/details/119954693
golang获取本月第一天、获取本月最后一天、获取当天0点的时间_liyunlong41的博客-CSDN博客_go 获取本月第一天 https://blog.csdn.net/liyunlong41/article/details/86766249
func main() {
currentTime := time.Now()
fmt.Println("当前时间 : ", currentTime)
fmt.Println("当前时间字符串: ", currentTime.String())
fmt.Println("MM-DD-YYYY : ", currentTime.Format("01-02-2006"))
fmt.Println("YYYY-MM-DD : ", currentTime.Format("2006-01-02"))
fmt.Println("YYYY.MM.DD : ", currentTime.Format("2006.01.02 15:04:05"))
fmt.Println("YYYY#MM#DD {Special Character} : ", currentTime.Format("2006#01#02"))
fmt.Println("YYYY-MM-DD hh:mm:ss : ", currentTime.Format("2006-01-02 15:04:05"))
fmt.Println("Time with MicroSeconds: ", currentTime.Format("2006-01-02 15:04:05.000000"))
fmt.Println("Time with NanoSeconds: ", currentTime.Format("2006-01-02 15:04:05.000000000"))
fmt.Println("ShortNum Month : ", currentTime.Format("2006-1-02"))
fmt.Println("LongMonth : ", currentTime.Format("2006-January-02"))
fmt.Println("ShortMonth : ", currentTime.Format("2006-Jan-02"))
fmt.Println("ShortYear : ", currentTime.Format("06-Jan-02"))
fmt.Println("LongWeekDay : ", currentTime.Format("2006-01-02 15:04:05 Monday"))
fmt.Println("ShortWeek Day : ", currentTime.Format("2006-01-02 Mon"))
fmt.Println("ShortDay : ", currentTime.Format("Mon 2006-01-2"))
fmt.Println("Short Hour Minute Second: ", currentTime.Format("2006-01-02 3:4:5"))
fmt.Println("Short Hour Minute Second: ", currentTime.Format("2006-01-02 3:4:5 PM"))
fmt.Println("Short Hour Minute Second: ", currentTime.Format("2006-01-02 3:4:5 pm"))
}
实践:
2)最近30天每日事件发生次数和
1)
SELECT
CONCAT(DATE_FORMAT(LAST_DAY(NOW() - INTERVAL 1 MONTH),'%Y-%m-'),'01'),
UNIX_TIMESTAMP(CONCAT(DATE_FORMAT(LAST_DAY(NOW() - INTERVAL 1 MONTH),'%Y-%m-'),'01'));
自然月 12个月 1年前零时
SELECT CURDATE(),LAST_DAY(NOW() - INTERVAL 12 MONTH),UNIX_TIMESTAMP(LAST_DAY(NOW() - INTERVAL 12 MONTH));
2023-02-10 2022-02-28 1645977600 2022-02-28 00:00:00
最近12个月零时时间戳
SELECT
UNIX_TIMESTAMP(CONCAT(DATE_FORMAT(LAST_DAY(NOW() - INTERVAL 0 MONTH),'%Y-%m-'),'01')),
UNIX_TIMESTAMP(CONCAT(DATE_FORMAT(LAST_DAY(NOW() - INTERVAL 1 MONTH),'%Y-%m-'),'01')),
UNIX_TIMESTAMP(CONCAT(DATE_FORMAT(LAST_DAY(NOW() - INTERVAL 2 MONTH),'%Y-%m-'),'01')),
UNIX_TIMESTAMP(CONCAT(DATE_FORMAT(LAST_DAY(NOW() - INTERVAL 3 MONTH),'%Y-%m-'),'01')),
UNIX_TIMESTAMP(CONCAT(DATE_FORMAT(LAST_DAY(NOW() - INTERVAL 4 MONTH),'%Y-%m-'),'01')),
UNIX_TIMESTAMP(CONCAT(DATE_FORMAT(LAST_DAY(NOW() - INTERVAL 5 MONTH),'%Y-%m-'),'01')),
UNIX_TIMESTAMP(CONCAT(DATE_FORMAT(LAST_DAY(NOW() - INTERVAL 6 MONTH),'%Y-%m-'),'01')),
UNIX_TIMESTAMP(CONCAT(DATE_FORMAT(LAST_DAY(NOW() - INTERVAL 7 MONTH),'%Y-%m-'),'01')),
UNIX_TIMESTAMP(CONCAT(DATE_FORMAT(LAST_DAY(NOW() - INTERVAL 8 MONTH),'%Y-%m-'),'01')),
UNIX_TIMESTAMP(CONCAT(DATE_FORMAT(LAST_DAY(NOW() - INTERVAL 9 MONTH),'%Y-%m-'),'01')),
UNIX_TIMESTAMP(CONCAT(DATE_FORMAT(LAST_DAY(NOW() - INTERVAL 10 MONTH),'%Y-%m-'),'01')),
UNIX_TIMESTAMP(CONCAT(DATE_FORMAT(LAST_DAY(NOW() - INTERVAL 11 MONTH),'%Y-%m-'),'01'))
;
CAST(UNIX_TIMESTAMP(CONCAT(DATE_FORMAT(LAST_DAY(NOW() - INTERVAL 0 MONTH),'%Y-%m-'),'01')) AS SIGNED),
converting driver.Value type []uint8 (\"1651334400.000000\") to a int32
SELECT
FROM_UNIXTIME(1650012615),
DATE_FORMAT(FROM_UNIXTIME(1650012615),"%Y%m"),
CAST(FROM_UNIXTIME(1650012615) AS DATE),
UNIX_TIMESTAMP(CAST(FROM_UNIXTIME(1650012615) AS DATE));
当前时间戳
SELECT NOW(),UNIX_TIMESTAMP(NOW());
mysql 本月第一天_mysql日期 获取本月第一天 获取下个月的第一天_liuyingliuyun的博客-CSDN博客 https://blog.csdn.net/weixin_31301993/article/details/113196770
select curdate(); --获取当前日期
select last_day(curdate()); --获取当月最后一天。
select DATE_ADD(curdate(),interval -day(curdate())+1 day); --获取本月第一天
select date_add(curdate()-day(curdate())+1,interval 1 month); -- 获取下个月的第一天
select DATEDIFF(date_add(curdate()-day(curdate())+1,interval 1 month ),DATE_ADD(curdate(),interval -day(curdate())+1 day)) from dual; --获取当前月的天数
#当年第一天:
SELECT DATE_SUB(CURDATE(),INTERVAL dayofyear(now())-1 DAY);
#当年最后一天:
SELECT concat(YEAR(now()),'-12-31');
#当前week的第一天:
select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) + 1 DAY);
#当前week的最后一天:
select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) - 5 DAY);
#前一week的第一天:
select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) + 8 DAY);
#前一week的最后一天:
select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) + 2 DAY);
#前两week的第一天:
select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) + 15 DAY);
#前两week的最后一天:
select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) + 9 DAY);
#当前month的第一天:
SELECT concat(date_format(LAST_DAY(now()),'%Y-%m-'),'01');
#当前month的最后一天:
SELECT LAST_DAY(now());
#前一month的第一天:
SELECT concat(date_format(LAST_DAY(now() - interval 1 month),'%Y-%m-'),'01');
#前一month的最后一天:
SELECT LAST_DAY(now() - interval 1 month);
#前两month的第一天:
SELECT concat(date_format(LAST_DAY(now() - interval 2 month),'%Y-%m-'),'01');
#前两month的最后一天:
SELECT LAST_DAY(now() - interval 2 month);
#当前quarter的第一天:
select concat(date_format(LAST_DAY(MAKEDATE(EXTRACT(YEAR FROM CURDATE()),1) + interval QUARTER(CURDATE())*3-3 month),'%Y-%m-'),'01');
#当前quarter的最后一天:
select LAST_DAY(MAKEDATE(EXTRACT(YEAR FROM CURDATE()),1) + interval QUARTER(CURDATE())*3-1 month);
#前一quarter的第一天:
select concat(date_format(LAST_DAY(MAKEDATE(EXTRACT(YEAR FROM CURDATE()),1) + interval QUARTER(CURDATE())*3-6 month),'%Y-%m-'),'01');
#前一quarter的最后一天:
select LAST_DAY(MAKEDATE(EXTRACT(YEAR FROM CURDATE()),1) + interval QUARTER(CURDATE())*3-4 month);
#前两quarter的第一天:
select concat(date_format(LAST_DAY(MAKEDATE(EXTRACT(YEAR FROM CURDATE()),1) + interval QUARTER(CURDATE())*3-9 month),'%Y-%m-'),'01');
#前两quarter的最后一天:
select LAST_DAY(MAKEDATE(EXTRACT(YEAR FROM CURDATE()),1) + interval QUARTER(CURDATE())*3-7 month);
from datetime import datetime, timedelta
d = datetime.now()
# 上月末最后一天
d1 = d + timedelta(days=-d.day)
# 上月第一天
d2 = d1 + timedelta(days=-d1.day + 1)
# 本月第一天
d3 = d1 + timedelta(days=-d.day + 1)
# 当日所在周的周一
monday = d + timedelta(days=-d.weekday())
s = monday.strftime('%Y-%m-%d')
SELECT * FROM ordertest_error_temp WHERE FROM_UNIXTIME(create_time,'%Y-%m-%d ')= CURDATE()
AND
( INSTR(url,'laiwunews.cn') OR INSTR(url,'.py168.com') OR INSTR(url,'xgrb.cn')
);
UPDATE ordertest_error_temp SET script_need_run_times=0,remarks=CONCAT('业务方需求',FROM_UNIXTIME(create_time,'%Y-%m-%d %H:%i:%s'),remarks)
WHERE FROM_UNIXTIME(create_time,'%Y-%m-%d ')= DATE_ADD(CURDATE(),INTERVAL 0 day) AND
( INSTR(url,'qhnews.com') OR INSTR(url,'laiwunews.cn') OR INSTR(url,'hizh.cn') OR INSTR(url,'xgrb.cn') OR INSTR(url,'py168.com')
OR
INSTR(url,'xyw.gov.cn') OR INSTR(url,'afinance.cn') OR INSTR(url,'szonline.net') OR INSTR(url,'88152.com') OR INSTR(url,'cnhan.com')
OR
INSTR(url,'wfdaily.com') OR INSTR(url,'ccin.com.cn') OR INSTR(url,'zznews.cn') OR INSTR(url,'bzcm.net')
);
SELECT COUNT(1) FROM ordertest WHERE NOT
( INSTR(url,'qhnews.com') OR INSTR(url,'laiwunews.cn') OR INSTR(url,'hizh.cn') OR INSTR(url,'xgrb.cn') OR INSTR(url,'py168.com')
OR
INSTR(url,'xyw.gov.cn') OR INSTR(url,'afinance.cn') OR INSTR(url,'szonline.net') OR INSTR(url,'88152.com') OR INSTR(url,'cnhan.com')
OR
INSTR(url,'wfdaily.com') OR INSTR(url,'ccin.com.cn') OR INSTR(url,'zznews.cn') OR INSTR(url,'bzcm.net')
);
mysql 时间戳 日期
SELECT FROM_UNIXTIME(1653372742,'%Y%m'),FROM_UNIXTIME(1653372742,'%Y%m%d'),
FROM_UNIXTIME(1653374892,'%Y-%m-%d %H:%i:%S'),TO_DAYS(NOW()) ,
DATE_FORMAT(CURDATE(),'%Y%m') ;
时间戳 月 月零时时间戳
SELECT FROM_UNIXTIME(1653372742,'%Y%m'),FROM_UNIXTIME(1653372742,'%Y-%m-01 00:00:00'),UNIX_TIMESTAMP(FROM_UNIXTIME(1653372742,'%Y-%m-01 00:00:00'))
函数:FROM_UNIXTIME
作用:将MYSQL中以INT(11)存储的时间以"YYYY-MM-DD"格式来显示。
语法:FROM_UNIXTIME(unix_timestamp,format)
返回表示 Unix 时间标记的一个字符串,根据format字符串格式化。format可以包含与DATE_FORMAT()函数列出的条目同样的修饰符。
根据format字符串格式化date值。
下列修饰符可以被用在format字符串中:
%M 月名字(January……December)
%W 星期名字(Sunday……Saturday)
%D 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。)
%Y 年, 数字, 4 位
%y 年, 数字, 2 位
%a 缩写的星期名字(Sun……Sat)
%d 月份中的天数, 数字(00……31)
%e 月份中的天数, 数字(0……31)
%m 月, 数字(01……12)
%c 月, 数字(1……12)
%b 缩写的月份名字(Jan……Dec)
%j 一年中的天数(001……366)
%H 小时(00……23)
%k 小时(0……23)
%h 小时(01……12)
%I 小时(01……12)
%l 小时(1……12)
%i 分钟, 数字(00……59)
%r 时间,12 小时(hh:mm:ss [AP]M)
%T 时间,24 小时(hh:mm:ss)
%S 秒(00……59)
%s 秒(00……59)
%p AM或PM
%w 一个星期中的天数(0=Sunday ……6=Saturday )
%U 星期(0……52), 这里星期天是星期的第一天
%u 星期(0……52), 这里星期一是星期的第一天
%% 一个文字“%”。
例子:
SELECT FROM_UNIXTIME(1234567890, '%Y-%m-%d %H:%i:%S')
SELECT *,FROM_UNIXTIME(created, '%Y-%m-%d') as riqi FROM `rc_ms_users`
/*****************************************************************************/
写过PHP+MySQL的程序员都知道有时间差,UNIX时间戳和格式化日期是我们常打交道的两个时间表示形式,Unix时间戳存储、处理方便,但是不直观,格式化日期直观,但是处理起来不如Unix时间戳那么自如,所以有的时候需要互相转换,下面给出互相转换的几种转换方式。
一、在MySQL中完成
这种方式在MySQL查询语句中转换,优点是不占用PHP解析器的解析时间,速度快,缺点是只能用在数据库查询中,有局限性。
1. UNIX时间戳转换为日期用函数: FROM_UNIXTIME()
一般形式:select FROM_UNIXTIME(1156219870);
2. 日期转换为UNIX时间戳用函数: UNIX_TIMESTAMP()
一般形式:Select UNIX_TIMESTAMP('2006-11-04 12:23:00′);
举例:mysql查询当天的记录数:
$sql=”select * from message Where DATE_FORMAT(FROM_UNIXTIME(chattime),'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d') order by id desc”;
当然大家也可以选择在PHP中进行转换,下面说说在PHP中转换。
二、在PHP中完成
这种方式在PHP程序中完成转换,优点是无论是不是数据库中查询获得的数据都能转换,转换范围不受限制,缺点是占用PHP解析器的解析时间,速度相对慢。
1. UNIX时间戳转换为日期用函数: date()
一般形式:date('Y-m-d H:i:s', 1156219870);
2. 日期转换为UNIX时间戳用函数:strtotime()
一般形式:strtotime('2010-03-24 08:15:42');
Golang时间戳 - 简书 https://www.jianshu.com/p/4db60205b029
// 获取月初时间
func GetEarlyMonthUnix() int64 {
now := time.Now()
tm := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
return tm.Unix()
}
// 获取零时时间
func GetZeroHourUnix() int64 {
now := time.Now()
tm := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
return tm.Unix()
}
// 获取当前小时时间
func GetNowHourUnix() int64 {
now := time.Now()
tm := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, now.Location())
return tm.Unix()
}
// 获取当前时间
func GetNowUnix() int64 {
return time.Now().Unix()
}
// 获取年初时间
func GetEarlyYearUnix() int64 {
now := time.Now()
tm := time.Date(now.Year(), 1, 1, 0, 0, 0, 0, now.Location())
return tm.Unix()
}
func GetUnixToFormatString(timestamp int64, f string) string {
tm := time.Unix(timestamp, 0)
return tm.Format(f)
}
func GetUnixToString(timestamp int64) string {