当前位置: 代码迷 >> python >> python中日期时间格式的问题
  详细解决方案

python中日期时间格式的问题

热度:110   发布时间:2023-07-16 11:14:32.0

我在格式化日期时遇到问题。 这是我的代码:

myDate=datetime.datetime.now()
print myDate #2011-02-02 13:54:26.162000
stime = time.mktime(time.strptime(myDate, '%Y-%m-%d %I:%M%S.%f'))

在最后一行,我得到了例外:

File "c:\Python27\Lib\_strptime.py", line 322, in _strptime
  found = format_regex.match(data_string)

这怎么了

编辑更清楚地说,这是我的整个代码,是我在stackoverflow上发现的内容的混合:

import random
import time

def strTimeProp(start, end, format, prop):
    """Get a time at a proportion of a range of two formatted times.

    start and end should be strings specifying times formated in the
    given format (strftime-style), giving an interval [start, end].
    prop specifies how a proportion of the interval to be taken after
    start.  The returned time will be in the specified format.
    """

    stime = time.mktime(time.strptime(start, format))
    etime = time.mktime(time.strptime(end, format))

    ptime = stime + prop * (etime - stime)

    return time.strftime(format, time.localtime(ptime))


def randomDate(start, end, prop):
    return strTimeProp(start, end, '%Y-%m-%d %H:%M:%S.%f', prop)

b1=datetime.datetime.now()
print b1
startDate=b1-datetime.timedelta(27375)
print startDate
endDate=b1-datetime.timedelta(6571)
print endDate
randomDate=randomDate(str(startDate), str(endDate), random.random())

我正在尝试获取成人的随机出生日期。

我正在使用Windows XP。

我正在尝试获取成人的随机出生日期。

好的,所以如果您说一个成年人可能在18到75岁之间(6571-27375天),那么让我们找到距离今天这么多天的日期:

from datetime import datetime, timedelta
import random

birthday = datetime.today() - timedelta(days = random.randrange(6571, 27375))

print 'Person was born on %s' % (birthday.strftime('%Y-%m-%d'))    

确实,您将遇到OverflowError因为您将超越unix时代。 一个简单的解决方案是将代码重构为使用datetime和timedelta对象,这样日期范围就不会有问题。 我自己进行了重构,它似乎可以正常工作(请注意,我没有考虑时区的子集)。

from datetime import datetime, timedelta
import random

def strTimeProp(start, end, format, prop):
    """Get a time at a proportion of a range of two formatted times.

    start and end should be strings specifying times formated in the
    given format (strftime-style), giving an interval [start, end].
    prop specifies how a proportion of the interval to be taken after
    start.  The returned time will be in the specified format.
    """

    sdatetime = datetime.strptime(start, format)
    edatetime = datetime.strptime(end, format)

    # get time delta and calculate new delta in days * prop
    delta = edatetime - sdatetime # this is a timedelta
    propdelta = timedelta(days = prop * delta.days)

    pdatetime = sdatetime + propdelta
    return pdatetime.strftime(format)


def randomDate(start, end, prop):
    return strTimeProp(start, end, '%Y-%m-%d %H:%M:%S.%f', prop)

b1=datetime.now()
print b1
startDate=b1-timedelta(27375)
print startDate
endDate=b1-timedelta(6571)
print endDate
randomDate=randomDate(str(startDate), str(endDate), random.random())
print randomDate

希望能帮助到你。

time.strptime()的第一个参数必须是字符串,而不是从datetime.datetime.now()返回的datetime对象。

将您的最后一行更改为:

stime = time.mktime(time.strptime(str(myDate), '%Y-%m-%d %H:%M:%S.%f'))

(但是,您编辑中的代码确实将datetime对象转换为字符串)