How to working with dates and times?

2024-06-18 23:08
文章标签 working times dates

本文主要是介绍How to working with dates and times?,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.How to formatting date for display?

Problem

You need to format dates or time for output

Solution

Use the date command with a strftime format specification.

3. How to automating date Ranges?

Problem

You have one date and you would like to generate the other automatically.

Solution

[maxwell@DBAMAXWELL cp11]$ date '+%Y-%m-%d %H:%M%S %z'
2022-03-19 09:5725 +0800
[maxwell@DBAMAXWELL cp11]$ date -d 'today' '+%Y-%m-%d %H:%M%S %z'
2022-03-19 09:5750 +0800
[maxwell@DBAMAXWELL cp11]$ date -d 'yesterday' '+%Y-%m-%d %H:%M%S %z'     
2022-03-18 09:5805 +0800
[maxwell@DBAMAXWELL cp11]$ date -d 'tomorrow' '+%Y-%m-%d %H:%M%S %z'         
2022-03-20 09:5821 +0800
[maxwell@DBAMAXWELL cp11]$ date -d 'Monday' '+%Y-%m-%d %H:%M%S %z'         
2022-03-21 00:0000 +0800
[maxwell@DBAMAXWELL cp11]$ date -d 'last Monday' '+%Y-%m-%d %H:%M%S %z'
2022-03-14 00:0000 +0800
[maxwell@DBAMAXWELL cp11]$ date -d 'next Monday' '+%Y-%m-%d %H:%M%S %z'    
2022-03-21 00:0000 +0800
[maxwell@DBAMAXWELL cp11]$ date -d 'last week' '+%Y-%m-%d %H:%M%S %z'              
2022-03-12 09:5943 +0800
[maxwell@DBAMAXWELL cp11]$ date -d 'next week' '+%Y-%m-%d %H:%M%S %z'    
2022-03-26 09:5954 +0800
[maxwell@DBAMAXWELL cp11]$ date -d '2 week' '+%Y-%m-%d %H:%M%S %z'    
2022-04-02 10:0005 +0800
[maxwell@DBAMAXWELL cp11]$ date -d '-2 weeks' '+%Y-%m-%d %H:%M%S %z'
2022-03-05 10:0025 +0800
[maxwell@DBAMAXWELL cp11]$ date -d '2 weeks' '+%Y-%m-%d %H:%M%S %z' 
2022-04-02 10:0034 +0800
[maxwell@DBAMAXWELL cp11]$ date -d '2 weeks ago' '+%Y-%m-%d %H:%M%S %z'
2022-03-05 10:0054 +0800
[maxwell@DBAMAXWELL cp11]$ date -d '+4 days' '+%Y-%m-%d %H:%M%S %z'           
2022-03-23 10:0121 +0800
[maxwell@DBAMAXWELL cp11]$ date -d '-6 days' '+%Y-%m-%d %H:%M%S %z'  
2022-03-13 10:0135 +0800
[maxwell@DBAMAXWELL cp11]$ date -d '2000-01-01 +12 days' '+%Y-%m-%d %H:%M%S %z'  
2000-01-13 00:0000 +0800
[maxwell@DBAMAXWELL cp11]$ date -d '3 months 1 day' '+%Y-%m-%d %H:%M%S %z'             
2022-06-20 10:0248 +0800
[maxwell@DBAMAXWELL cp11]$ date '+%a %Y-%m-%d'
Sat 2022-03-19
[maxwell@DBAMAXWELL cp11]$ date -d 'today' '+%a %Y-%m-%d'
Sat 2022-03-19
[maxwell@DBAMAXWELL cp11]$ date -d 'Saturday' '+%a %Y-%m-%d'     
Sat 2022-03-19
[maxwell@DBAMAXWELL cp11]$ date -d 'last Saturday' '+%a %Y-%m-%d'
Sat 2022-03-12
[maxwell@DBAMAXWELL cp11]$ date -d 'this Saturday' '+%a %Y-%m-%d'    
Sat 2022-03-19
[maxwell@DBAMAXWELL cp11]$ date -d 'next Saturday' '+%a %Y-%m-%d'    
Sat 2022-03-26
[maxwell@DBAMAXWELL cp11]$ date -d 'this week Friday' '+%a %Y-%m-%d'              
Fri 2022-03-25
[maxwell@DBAMAXWELL cp11]$ 

3. How to converting Dates and Times to Epoch Seconds

[maxwell@DBAMAXWELL cp11]$ date '+%s'
1647657592
[maxwell@DBAMAXWELL cp11]$ date -d '2005-11-05 12:00:00 +0000' '+%s'
1131192000
[maxwell@DBAMAXWELL cp11]$ perl -e 'print time, qq(\n);'
1647657672
[maxwell@DBAMAXWELL cp11]$ perl -e 'use Time::Local; print timelocal(localtime( )) . qq(\n);'
1647657680
[maxwell@DBAMAXWELL cp11]$ perl -e 'use Time::Local; print timelocal(localtime( )) . qq(\n);'
1647657694
[maxwell@DBAMAXWELL cp11]$ perl -e 'use Time::Local; print timelocal("49", "59", "06", "05", "10", "105") .
> qq(\n);'
1131145189
[maxwell@DBAMAXWELL cp11]$ perl -e 'use Time::Local; print timegm("49", "59", "06", "05", "10", "105") . qq(\
> n);'
1131173989
n[maxwell@DBAMAXWELL cp11]$

3. How to converting Epoch Seconds to Dates and Times

n[maxwell@DBAMAXWELL cp11]$ EPOCH='1131173989'
[maxwell@DBAMAXWELL cp11]$ date -d "1970-01-01 UTC $EPOCH seconds" +"%Y-%m-%d %T %z"
2005-11-05 14:59:49 +0800
[maxwell@DBAMAXWELL cp11]$ date --utc --date "1970-01-01 $EPOCH seconds" +"%Y-%m-%d %T %z"
2005-11-05 06:59:49 +0000
[maxwell@DBAMAXWELL cp11]$ 

4.How to getting yesterday or tomorrow with Perl

[maxwell@DBAMAXWELL cp11]$ perl -e "use POSIX qw(strftime); print strftime('%Y-%m-%d', localtime(time -
> 86400)), qq(\n);"

2022-03-18
[maxwell@DBAMAXWELL cp11]$ perl -e "use POSIX qw(strftime); print strftime('%Y-%m-%d', localtime(time +
> 86400)), qq(\n);"

2022-03-20
[maxwell@DBAMAXWELL cp11]$ 

这篇关于How to working with dates and times?的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1073315

相关文章

Eclipse或MyEclipse中Java Working Set管理项目

随着学习JAVA的时间的越来越久,项目也越来越多,Eclipse或MyEclipse界面中显示一堆! 每次工作使用到的项目肯定不会太多...... 每次从这么大数量的工程当中找到自己要使用的, 必须大规模的滚动滚动条...... 图片一   Project Explorer中:    图片二:Package Explorer中: 这样就好找很多了,分类放!

Working with excel files using Pandas

https://www.geeksforgeeks.org/working-with-excel-files-using-pandas/

Permute K times

题目链接:Permute K times 首先我们考虑暴力的作法,就是将一个点走k步,最后所在的那个点即为所求,考虑数据范围显然不可以,因此我们可以采用倍增的写法去实现。每个点的步数转化成二进制形式去求,用st表。 代码: #include <bits/stdc++.h>#define int long long#define fi first#define se secondusi

hadoop执行分词时报错:System times on machines may be out of sync. Check system time and time zones.

解决办法: 1、安装ntpdate工具 yum -y install ntp ntpdate 2、 设置系统时间与网络时间同步 ntpdate cn.pool.ntp.org

Ubuntu 18.04 This page isn’t working xxx.com didn’t send any data. ERR_EMPTY_RESPONSE

Ubuntu18.04 chrome Firefox 都试了一遍 提示如下: This page isn’t working xxx.com didn’t send any data. ERR_EMPTY_RESPONSE 本地 curl xxx.com 都可以出数据,找了一天的问题,最终发现是 Network-> Network Proxy 网络有代理 http://xxx.xxx.

Os bootup and stop working when the / directory is ful

From the Gnome UI, there is no response in the command line. So telnet from other machine, and cleanup and make up some space in / dir, it will work as normal.

Working with Containers

[外链图片转存失败(img-fui4VSTG-1566044208471)(http://7xizmp.com1.z0.glb.clouddn.com/Docker-colorful.png)] ##回顾之前学习过的一些命令 docker run -i...交互式运行docker run -d...background运行`daemon`守护进程docker ps...Lists cont

svn working copy locked问题

问题描述: 用svn在项目文件夹下commit或者update时会出现错误提示“working copy locked” 解决: 1、在项目文件夹下,单机鼠标右键,选择tortoisesvn-》cleanup; 2、如果cleanup没有效果的话只好手动删除锁定文件。 cd 到svn项目目录下,然后执行如下命令 del lock /q/s E:\项目\卫生局\监督局

svn 无法提交,报错Solve svn : Working copy is too old (format 10, created by Subversion 1.6)

今儿做东西,svn除了问题。问题是 1.svn无法提交。选中文件,右键svn都没有commit的提示。后来发现在该文件夹下,都没有svn控制的那些符号,比如问号,感叹号之类的。最后,是因为我当时从svncheck out下来是check到桌面,后来又移动该文件夹到其他地方,那么当初记录的路径已经改变了,自然不会再接受svn的管理,也就无法提交了。解决办法,想了一个笨办法,从新从svn上ch