Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
string transformation
#1
Hi,

Could you tell me the most pythonic way to make this transformation:

day='181212'
newday='2018-12-12'
I did this, but I don't like it:
newday = '20' + day[0] + day[1] + '-' + day[2] + day[3] + '-' + day[4] + day[5]
Thanks
Reply
#2
I would use
import datetime
day = '181212'
newday = datetime.datetime.strptime(day, '%y%d%m').strftime('%Y-%d-%m')
Note that 12 12 is ambiguous. Is it day/month or month/day?
Reply
#3
Thanks. It's gonna be %Y-%m-%d format.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  point transformation software in space johnjsi 2 394 Feb-01-2024, 01:31 AM
Last Post: johnjsi
  Calculate transformation and Rotation Sandra2312 1 1,816 Jan-31-2021, 12:53 PM
Last Post: jefsummers
  regular expression for a transformation bluefrog 1 2,943 May-30-2018, 11:53 AM
Last Post: killerrex
  Automation XML transformation vasilysmyslov 0 2,146 Aug-05-2017, 06:32 PM
Last Post: vasilysmyslov

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020