0

I have string like this:

2019-04-03 05:10:35+03:00

I need to output date like this:

2019-04-03 08:10:35

My code:

print(datetime.strptime(str("2019-04-03 05:10:35+03:00"), "%Y-%m-%d %H:%M:%S%z"))

but I have error:

ValueError: time data '2019-04-03 05:10:35+03:00' does not match format '%Y-%m-%d %H:%M:%S%z'

DirtyBit
  • 16,613
  • 4
  • 34
  • 55
nesalexy
  • 848
  • 2
  • 9
  • 30

3 Answers3

2

This will work

from datetime import datetime

your_date = datetime.strptime("2019-04-03 05:10:35+03:00", "%Y-%m-%d %H:%M:%S%z")

print(your_date.strftime("%Y-%m-%d %H:%M:%S"))

enter image description here

EDIT:

You have "+03:00" if you count timezone, if you want to add that to your result, do it like this:

from datetime import datetime
your_date = datetime.strptime("2019-04-03 05:10:35+03:00", "%Y-%m-%d%H:%M:%S%z")
print((your_date + timedelta(0, your_date.tzinfo.utcoffset(your_date).seconds)).strftime("%Y-%m-%d %H:%M:%S"))

enter image description here

1

The problem is that your input string is improperly formatted. %z expects a string of format +HHMM or -HHMM; you have an extra :.

Accordingly, you could use a regex to format it:

import re

source = '2019-04-03 05:10:35+03:00'
formatted = re.sub(r'([+-])(\d\d):(\d\d)', r'\1\2\3', source)

print(datetime.strptime(formatted, "%Y-%m-%d %H:%M:%S%z").astimezone(timezone.utc).strftime("%Y-%m-%d %H:%M:%S"))

Output:

2019-04-03 02:10:35
gmds
  • 19,325
  • 4
  • 32
  • 58
  • 1
    The output is incorrect. – DirtyBit Apr 04 '19 at 10:27
  • thanks for answer! How i can output instead `2019-04-03 05:10:35+03:00` date like this `2019-04-03 08:10:35` (plus or minus hours) – nesalexy Apr 04 '19 at 10:31
  • 1
    @nesalexy To clarify, is your expected output `08:10:35` or `02:10:35`? (since +03:00 normally implies that the given time is *ahead* of UTC) – gmds Apr 04 '19 at 10:31
  • 1
    The output is still incorrect. – DirtyBit Apr 04 '19 at 10:33
  • 1
    @DirtyBit I may have misunderstood the question, then... – gmds Apr 04 '19 at 10:36
  • @gmds honestly if we take date like this `2019-04-03 05:10:35+03:00` i thought we need +3 hrs. For example if our date like this `2019-04-03 05:10:35-03:00` i thought we need -3hrs – nesalexy Apr 04 '19 at 10:37
  • 1
    @nesalexy If you say the time now is 10 PM UTC+8, the only other conventional way to represent it would be in UTC, which would be 2 PM. To put it another way, the `+03:00` after the time normally signifies a *timezone* in relation to UTC. For you to interpret it as an *offset stored in the same piece of data* instead is not wrong, but it's a little unusual, so it would be good to get some clarity on what your actual usecase is. – gmds Apr 04 '19 at 10:39
1

Using datetime and split() for str manipulation:

Assuming it to be + hours:

from datetime import datetime, timedelta

dt_1 = "2019-04-03 05:10:35+03:00"
date_ = dt_1.split("+")[0]
time_ = date_.split(" ")[1]
to_add = dt_1.split("+")[1]

d = datetime.strptime(date_, "%Y-%m-%d %H:%M:%S")
t = datetime.strptime(time_, "%H:%M:%S")
d += timedelta(hours=int(to_add.split(":")[0]), minutes=int(to_add.split(":")[1]))

print(d)

OUTPUT:

2019-04-03 08:10:35
DirtyBit
  • 16,613
  • 4
  • 34
  • 55