<small id='Vg6FE'></small><noframes id='Vg6FE'>

<legend id='Vg6FE'><style id='Vg6FE'><dir id='Vg6FE'><q id='Vg6FE'></q></dir></style></legend>

      <i id='Vg6FE'><tr id='Vg6FE'><dt id='Vg6FE'><q id='Vg6FE'><span id='Vg6FE'><b id='Vg6FE'><form id='Vg6FE'><ins id='Vg6FE'></ins><ul id='Vg6FE'></ul><sub id='Vg6FE'></sub></form><legend id='Vg6FE'></legend><bdo id='Vg6FE'><pre id='Vg6FE'><center id='Vg6FE'></center></pre></bdo></b><th id='Vg6FE'></th></span></q></dt></tr></i><div id='Vg6FE'><tfoot id='Vg6FE'></tfoot><dl id='Vg6FE'><fieldset id='Vg6FE'></fieldset></dl></div>
      • <bdo id='Vg6FE'></bdo><ul id='Vg6FE'></ul>
      <tfoot id='Vg6FE'></tfoot>

        如何在python中将日期时间转换为整数

        How to convert datetime to integer in python(如何在python中将日期时间转换为整数)
        <tfoot id='U62kd'></tfoot>
          <tbody id='U62kd'></tbody>
      1. <i id='U62kd'><tr id='U62kd'><dt id='U62kd'><q id='U62kd'><span id='U62kd'><b id='U62kd'><form id='U62kd'><ins id='U62kd'></ins><ul id='U62kd'></ul><sub id='U62kd'></sub></form><legend id='U62kd'></legend><bdo id='U62kd'><pre id='U62kd'><center id='U62kd'></center></pre></bdo></b><th id='U62kd'></th></span></q></dt></tr></i><div id='U62kd'><tfoot id='U62kd'></tfoot><dl id='U62kd'><fieldset id='U62kd'></fieldset></dl></div>

                <small id='U62kd'></small><noframes id='U62kd'>

                  <bdo id='U62kd'></bdo><ul id='U62kd'></ul>

                  <legend id='U62kd'><style id='U62kd'><dir id='U62kd'><q id='U62kd'></q></dir></style></legend>
                • 本文介绍了如何在python中将日期时间转换为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如何在 python 中将 YYYY-MM-DD hh:mm:ss 格式转换为整数?例如 2014-02-12 20:51:14 ->为整数.

                  How can I convert YYYY-MM-DD hh:mm:ss format to integer in python? for example 2014-02-12 20:51:14 -> to integer.

                  我只知道如何转换 hh:mm:ss 而不是 yyyy-mm-dd hh:mm:ss

                  I only know how to convert hh:mm:ss but not yyyy-mm-dd hh:mm:ss

                  def time_to_num(time_str):
                      hh, mm , ss = map(int, time_str.split(':'))
                      return ss + 60*(mm + 60*hh)
                  

                  推荐答案

                  这取决于整数应该编码的内容.您可以将日期转换为以前某个时间的毫秒数.人们经常在 1970 年 1 月 1 日凌晨 12:00 或 1900 年等时间执行此操作,并将时间测量为从该点开始的整数毫秒数.datetime 模块(或其他类似模块)将具有为您执行此操作的函数:例如,您可以使用 int(datetime.datetime.utcnow().timestamp()).

                  It depends on what the integer is supposed to encode. You could convert the date to a number of milliseconds from some previous time. People often do this affixed to 12:00 am January 1 1970, or 1900, etc., and measure time as an integer number of milliseconds from that point. The datetime module (or others like it) will have functions that do this for you: for example, you can use int(datetime.datetime.utcnow().timestamp()).

                  如果您想对年、月和日进行语义编码,一种方法是将这些组件乘以足够大的数量级值,以将它们并列在整数位内:

                  If you want to semantically encode the year, month, and day, one way to do it is to multiply those components by order-of-magnitude values large enough to juxtapose them within the integer digits:

                  2012-06-13 --> 20120613 = 10,000 * (2012) + 100 * (6) + 1*(13)

                  2012-06-13 --> 20120613 = 10,000 * (2012) + 100 * (6) + 1*(13)

                  def to_integer(dt_time):
                      return 10000*dt_time.year + 100*dt_time.month + dt_time.day
                  

                  例如

                  In [1]: import datetime
                  
                  In [2]: %cpaste
                  Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
                  :def to_integer(dt_time):
                  :    return 10000*dt_time.year + 100*dt_time.month + dt_time.day
                  :    # Or take the appropriate chars from a string date representation.
                  :--
                  
                  In [3]: to_integer(datetime.date(2012, 6, 13))
                  Out[3]: 20120613
                  

                  如果您还需要分钟和秒,则只需根据需要包含更多数量级以显示数字.

                  If you also want minutes and seconds, then just include further orders of magnitude as needed to display the digits.

                  我在遗留系统中经常遇到第二种方法,尤其是从遗留 SQL 数据库中提取基于日期的数据的系统.

                  I've encountered this second method very often in legacy systems, especially systems that pull date-based data out of legacy SQL databases.

                  非常糟糕.您最终会编写大量用于对齐日期、计算月份或日期偏移量的 hacky 代码,因为它们将以整数格式出现(例如,当您经过 12 月时将月份重置回 1,然后增加年份值),以及用于整数格式之间的相互转换.

                  It is very bad. You end up writing a lot of hacky code for aligning dates, computing month or day offsets as they would appear in the integer format (e.g. resetting the month back to 1 as you pass December, then incrementing the year value), and boiler plate for converting to and from the integer format all over.

                  除非这样的约定存在于您正在处理的 API 的深层、低级且经过全面测试的部分中,否则曾经使用过数据的每个人都可以真正依靠这种整数表示及其所有辅助函数,那么你最终会导致很多人到处重写基本的日期处理例程.

                  Unless such a convention lives in a deep, low-level, and thoroughly tested section of the API you're working on, such that everyone who ever consumes the data really can count on this integer representation and all of its helper functions, then you end up with lots of people re-writing basic date-handling routines all over the place.

                  通常最好将值保留在日期上下文中,如 datetime.date,尽可能长,以便对其进行的操作以自然的日期表示 -基于上下文,而不是某个单独的开发人员个人破解整数.

                  It's generally much better to leave the value in a date context, like datetime.date, for as long as you possibly can, so that the operations upon it are expressed in a natural, date-based context, and not some lone developer's personal hack into an integer.

                  这篇关于如何在python中将日期时间转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Running .jl file from R or Python(从 R 或 Python 运行 .jl 文件)
                  Running Julia .jl file in python(在 python 中运行 Julia .jl 文件)
                  Using PIP in a Azure WebApp(在 Azure WebApp 中使用 PIP)
                  How to run python3.7 based flask web api on azure(如何在 azure 上运行基于 python3.7 的烧瓶 web api)
                  Azure Python Web App Internal Server Error(Azure Python Web 应用程序内部服务器错误)
                  Run python dlib library on azure app service(在 azure app 服务上运行 python dlib 库)

                    <tbody id='WnZXd'></tbody>

                  • <bdo id='WnZXd'></bdo><ul id='WnZXd'></ul>

                        <i id='WnZXd'><tr id='WnZXd'><dt id='WnZXd'><q id='WnZXd'><span id='WnZXd'><b id='WnZXd'><form id='WnZXd'><ins id='WnZXd'></ins><ul id='WnZXd'></ul><sub id='WnZXd'></sub></form><legend id='WnZXd'></legend><bdo id='WnZXd'><pre id='WnZXd'><center id='WnZXd'></center></pre></bdo></b><th id='WnZXd'></th></span></q></dt></tr></i><div id='WnZXd'><tfoot id='WnZXd'></tfoot><dl id='WnZXd'><fieldset id='WnZXd'></fieldset></dl></div>

                        <legend id='WnZXd'><style id='WnZXd'><dir id='WnZXd'><q id='WnZXd'></q></dir></style></legend>

                        <small id='WnZXd'></small><noframes id='WnZXd'>

                            <tfoot id='WnZXd'></tfoot>