python中格式化日期时间的问题

时间:2011-02-02 13:02:25

标签: python datetime

格式化日期有问题。这是我的代码:

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。

3 个答案:

答案 0 :(得分:4)

我正在尝试为成年人提供随机出生日期。

好的,所以如果你说一个成年人可以在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'))    

答案 1 :(得分:3)

事实上,你会点击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

希望它有所帮助。

答案 2 :(得分:0)

time.strptime()的第一个参数需要是一个字符串,而不是从datetime

返回的datetime.datetime.now()对象

将您的最后一行更改为:

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

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