-1

How to convert string which I need to UTC? 06-09-2019 14:34 to 06/09/2019 14:34

date2 = datetime.datetime.strptime(context.beginning_date_attribute, '%d/%m/%Y %H:%M')

this code produces 2019-09-06 14:45:00 and I dont want seconds also here

Corey Goldberg
  • 59,062
  • 28
  • 129
  • 143
ranger
  • 637
  • 2
  • 9
  • 18

3 Answers3

1

I'm not sure what context.beginning_date_attribute is, but here is how to reformat your timestamp.

from datetime import datetime

GMT_timestamp = datetime.utcnow()
print (GMT_timestamp)
# outputs 
2019-09-06 14:05:22.215443

# milliseconds removed from GMT timestamp
reformatted_GMT_timestamp = GMT_timestamp.strftime('%Y-%m-%d %H:%M')
print (reformatted_GMT_timestamp)
# outputs 
2019-09-06 14:05

reformatted_timestamp = datetime.strptime('2019-09-06 14:05:22.215443', '%Y-%m-%d 
%H:%M:%S.%f').strftime('%d/%m/%Y %H:%M')
print (reformatted_timestamp)
# outputs 
06/09/2019 14:05
Life is complex
  • 15,374
  • 5
  • 29
  • 58
  • context.beginning_date_attribute = UTC +2 in this format 09/09/2019 06:25 – ranger Sep 09 '19 at 06:29
  • ```date_for_beginning_date = datetime.strptime(context.beginning_date_attribute, '%d/%m/%Y %H:%M').strftime('%d/%m/%Y %H:%M')``` == 09/09/2019 10:10 its great !! I just need to convert it to UTC – ranger Sep 09 '19 at 08:13
  • @ranger thanks for letting me know about the date string. I'm glad that I could help you in someway. If I did help you please accept the answer or upvote the answer. – Life is complex Sep 09 '19 at 13:27
0

Try using replace

date2.replace("-", "/")
Mate Mrše
  • 7,997
  • 10
  • 40
  • 77
0

You can use it like this:

from datetime import datetime
# current date and time
now = datetime.now()

s1 = now.strftime("%m/%d/%Y, %H:%M:%S")
# mm/dd/YY H:M:S format
0xM4x
  • 460
  • 1
  • 8
  • 19
  • thanks but I want to pass string with date not current date and also no seconds needed just '%d/%m/%Y %H:%M' – ranger Sep 06 '19 at 13:11
  • ``` utc_offset = datetime.utcnow() - datetime.now() local_datetime = datetime.strptime(raw_date, '%d/%m/%Y %H:%M') local_to_utc_datetime = local_datetime + utc_offset return local_to_utc_datetime.strftime('%d/%m/%Y %H:%M') this works for me ``` – ranger Sep 10 '19 at 08:09