1.导入包  import datetime

2.获取当前的时间

curr_time = datetime.datetime.now()  # 2019-07-06 14:55:56.873893 <class 'datetime.datetime'>
curr_time.year  # 2019 <class 'int'>
curr_time.month  # 7 <class 'int'>
curr_time.day  # 6 <class 'int'>
curr_time.hour  # 14 <class 'int'>
curr_time.minute  # 55 <class 'int'>
curr_time.second  # 56 <class 'int'>

curr_time.date()  # 2019-07-06 <class 'datetime.date'>

3.格式化

通过datetime.datetime.now(),我们获取到的时间格式为:2019-07-06 14:55:56.873893,类型:<class 'datetime.datetime'>

我们可以使用strftime()转换成我们想要的格式。处理之后的返回的值为2019-07-06、07/06等目标形式,类型为str

time_str = curr_time.strftime("%Y-%m-%d")  # 2019-07-06
time_str = curr_time.strftime("%m/%d")  # 07/06

4.类型转换

时间一般通过:时间对象,时间字符串,时间戳表示

通过datetime获取到的时间变量,类型为:datetime,那么datetime与str类型如何互相转换呢?

datetime-->str

time_str = datetime.datetime.strftime(curr_time,'%Y-%m-%d %H:%M:%S')  # 2019-07-06 15:50:12
str-->datetime
time_str = '2019-07-06 15:59:58'
curr_time = datetime.datetime.strptime(time_str,'%Y-%m-%d %H:%M:%S')

python 如何获取当前系统的时间的更多相关文章

  1. Java 获取当前系统的时间

    获取当前系统的时间,每隔一秒,打印一次. import java.util.Date; public class TestDate { public static void main(String[] ...

  2. java/python中获取当前系统时间,并与字符串相互转换格式,或者转化成秒数,天数等整数

    java转换成秒数 Date类有一个getTime()可以换回秒数,例如: public class DateToSecond { public static void main(String[] a ...

  3. python基础——13(系统、时间、序列化模块)

    一.时间模块 1.标准库time %y 两位数的年份表示(00-99) %Y 四位数的年份表示(0000-9999) %m 月份(01-12) %d 月中的一天(0-31) %H 24小时制小时数(0 ...

  4. java 获取当前系统系时间

    //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式 SimpleDateFo ...

  5. Android开发之java代码中获取当前系统的时间工具类

    /** * 获取当前时间 * * @return */ public String getTime() { Date date = new Date();// 创建一个时间对象,获取到当前的时间 Si ...

  6. linux获取当前系统的时间

    #include <time.h> #include <sys/time.h> void sysLocalTime(char *str_info) { time_t times ...

  7. cocos2d-x JS 获取当前系统时间(解决屏幕双击点击事件)

    记录一下,好开心,感觉今天自己又学到东西了,对于屏幕双击事件本来还毫无头绪的,今天得以解决总算没白费加班,其实原理很简单:就是在点击事件里做一个判断,这个判断就是需要获取当前系统的时间的毫秒差,第一次 ...

  8. C/C++获取系统当前时间

    C/C++获取系统当前时间   C库中与系统时间相关的函数定义在<time.h>头文件中, C++定义在<ctime>头文件中. 一.time(time_t*)函数 函数定义如 ...

  9. C++获取当前系统时间并格式化输出

    C++中与系统时间相关的函数定义在头文件中. 一.time(time_t * )函数 函数定义如下: time_t time (time_t* timer); 获取系统当前日历时间 UTC 1970- ...

随机推荐

  1. 【LeetCode】437. Path Sum III 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS + DFS BFS + DFS 日期 题目地 ...

  2. 【LeetCode】658. Find K Closest Elements 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/find-k-c ...

  3. sql注入-整数型

    sql注入整数型   1.按照提示输入1,发现直接给出了SQL语句   2.使用order by判断字段数   首先使用order by 3 ,页面无回显,改为2之后页面显示正确.因此判断当前数据库当 ...

  4. isEmpty 和 isBlank 的区别

    一般使用Apache commons-lang3 工具包: commons-lang3 是专业的工具包,功能非常齐全.强大. 1.isEmpty 判断字符串是否为空字符串,只要有一个任意字符(包括空白 ...

  5. Tcpdump抓包命令使用

    tcpdump命令需要使用root执行 1. 查看网卡命令 ifconfig 2. 监视编址到指定端口的TCP或UDP数据包,那么执行以下命令: tcpdump -i eth0 host 10.43. ...

  6. visual studio code 修改工具栏风格

    用windows版vscode的同学们是否发现它的工具栏是白色的跟整个界面看起来不太搭调,如下图: 其实要改变标题栏颜色也很简单,点击:文件> 首选项>设置 将 "window. ...

  7. Python_多任务:进程、线程、协程

    进程 进程是一个具有一定独立功能的程序在一个数据集上的一次动态执行的过程,是操作系统进行资源分配和调度的一个独立单位,是应用程序运行的载体.进程是一种抽象的概念,从来没有统一的标准定义.进程一般由程序 ...

  8. Redis_简介(1)

    Redis简介 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.从2010年3月15日起,Redis的开发工作 ...

  9. JQuery实现奇偶行不同背景颜色

    假设table的id为:t1 $(document).ready(function(){ $('#t1 > tbody tr:even').css('background','white'); ...

  10. 第10组 Alpha冲刺 (4/6)

    1.1基本情况 ·队名:今晚不睡觉 ·组长博客:https://www.cnblogs.com/cpandbb/p/13982696.html ·作业博客:https://edu.cnblogs.co ...