wxpython> DatePickerCtrl obj - 如何将'year'从2位数转换为4位数?

时间:2015-11-20 15:38:47

标签: python datepicker wxpython

问题

  

在GUI DatePickerCtrl obj中,日期格式为:mm / dd / yyyy

     

如果我得到DatePickerCtrl obj值,则日期格式为:mm / dd / yy hh:mm:ss

CODE代码段

datePicker = wx.adv.DatePickerCtrl( frame, wx.ID_ANY, wx.DefaultDateTime)
print datePicker.GetValue() # This outputs  11/20/15 14:20:55

我可以转换为字符串和切片[:-8],但我无法将 '15'转换为'2015'

当前

11/20/15

DESIRED

11/20/2015

3 个答案:

答案 0 :(得分:2)

您需要使用wx.DateTime.Format()

datePicker = wx.DatePickerCtrl( frame, wx.ID_ANY, wx.DefaultDateTime)
sel_date = datePicker.GetValue()
print sel_date.Format("%d-%m-%Y")

P.S。什么是在wx.adv.DatePickerCtrl中间做什么?

答案 1 :(得分:1)

<{> 1}} GetValue方法返回DatePickerCtrl对象,因此答案很简单,如下所示。

wx.DateTime

以下是dpc = wx.DatePickerCtrl(self) datetime_obj = dpc.GetValue() datetime_obj.GetYear() # -> returns year as four digit integer 的{​​{3}}。您也可以轻松获得月份(wx.DateTime)和日期(GetMonth()),小时,分钟等。

答案 2 :(得分:0)

问题解决了。

import time
fullTime = str(datePicker.GetValue())[:-8]
print fullTime
firstHalf = fullTime[:6]
print "firstHalf is: " , firstHalf
secondHalf = fullTime[-3:]
print "secondHalf", secondHalf
year = int(secondHalf)
if 59 <= year <= 99:
  secondHalf = '19' + secondHalf
else:
  secondHalf = '20' + secondHalf
print "altered_secondHalf", secondHalf
finalTime = firstHalf + secondHalf
print finalTime