Home > Enterprise >  extract date, month and year from string in python
extract date, month and year from string in python

Time:08-02

I have this column where the string has date, month, year and also time information. I need to take the date, month and year only.

There is no space in the string.

The string is on this format:

date
Tuesday,August22022-03:30PMWIB
Monday,July252022-09:33PMWIB
Friday,January82022-09:33PMWIB

and I expect to get:

date
2022-08-02
2022-07-25
2022-01-08

How can I get the date, month and year only and change the format into yyyy-mm-dd in python?

thanks in advance

CodePudding user response:

You can use the standard datetime library

from datetime import datetime

dates = [
    "Tuesday,August22022-03:30PMWIB",
    "Monday,July252022-09:33PMWIB",
    "Friday,January82022-09:33PMWIB"
]

for text in dates:
    text = text.split(",")[1].split("-")[0]
    dt = datetime.strptime(text, '%B%d%Y')
    print(dt.strftime("%Y-%m-%d"))

An alternative/shorter way would be like this (if you want the other date parts):

for text in dates:
    dt = datetime.strptime(text[:-3], '%A,%B%d%Y-%I:%M%p')
    print(dt.strftime("%Y-%m-%d"))

The timezone part is tricky and works only for UTC, GMT and local. You can read more about the format codes here.

strptime() only accepts certain values for %Z:

any value in time.tzname for your machine’s locale

the hard-coded values UTC and GMT

CodePudding user response:

You can convert to datetime object then get string back.

from datetime import datetime
datetime_object = datetime.strptime('Tuesday,August22022-03:30PM', '%A,%B%d%Y-%I:%M%p')
s = datetime_object.strftime("%Y-%m-%d")
print(s)

CodePudding user response:

Use strptime from datetime library

var = "Tuesday,August22022-03:30PMWIB"
date = var.split('-')[0]
formatted_date = datetime.strptime(date, "%A,%B%d%Y")
print(formatted_date.date()) #this will get your output

Output:

2022-08-02

CodePudding user response:

You can use the datetime library to parse the date and print it in your format. In your examples the day might not be zero padded so I added that and then parsed the date.

import datetime

date = 'Tuesday,August22022-03:30PMWIB'
date = date.split('-')[0]

if not date[-6].isnumeric():
    date = date[:-5]   "0"   date[-5:]
    newdate = datetime.datetime.strptime(date, '%A,%B%d%Y').strftime('%Y-%m-%d')
    print(newdate)
    # prints 2022-08-02
  • Related