使用python或VB Script将月/日/年转换为年份

时间:2014-07-07 22:35:52

标签: python vbscript

数据格式如下: 2014年5月1日,并希望将该日期格式转换为单年整数(短)

1 个答案:

答案 0 :(得分:1)

的VBScript:

将输入字符串的相应部分(Split)转换为数字:

>> sDate = "05/01/2014"
>> iYear = CInt(Split(sDate, "/")(2))
>> WScript.Echo TypeName(iYear), iYear
>>
Integer 2014

将输入字符串转换为日期(依赖于区域设置)并使用Year函数:

>> dtDate = CDate(sDate)
>> iYear = Year(dtDate)
>> WScript.Echo TypeName(iYear), iYear
>>
Integer 2014

Python(2.5,用于打印):

via string / int conv(c)@false):

>>> sDate = "01/05/2014"
>>> iYear = int(sDate.split("/")[-1])
>>> print type(iYear), iYear
<type 'int'> 2014

通过日期(时间):

>>> import datetime
>>> iYear = datetime.datetime.strptime('05/01/2014', '%m/%d/%Y').year
>>> print type(iYear), iYear
<type 'int'> 2014
相关问题