2

So I have been learning or pretty much just playing around with the time where I managed to get I believe is ISOformat and I for some "dumb" reason can't translate it to a datetime from a ISOformat and I looked into

datetime.datetime.strptime
datetime.datetime.strftime

and I could easily say that i'm already lost from thinking on what to do so here I am.

Etc: I have a time that looks like

2018-03-08T08:00:00.000

and want it basically to look:

2018-03-08 08:00:00

meaning removing the T and the 3 last micro seconds.

What is the best suggestion to do it?

smci
  • 32,567
  • 20
  • 113
  • 146
CDNthe2nd
  • 369
  • 1
  • 5
  • 19
  • 2
    First, both of those are valid ISO-format datetimes. The `T` separator is optional, and so are the milliseconds. Next, are you trying to get a `datetime` object, or a string in the second format? Your question is kind of ambiguous as to which you want. Finally, you don't have microseconds, you have milliseconds. – abarnert Jun 27 '18 at 23:57
  • oh Im sorry! well basically I just want it to look like `2018-03-08 08:00:00` Im sorry for saying the wrong micro instead of milli. Really stupid of me but that's what being up late cause. Basically I just want to translate it from ISO to datetime. That's it. – CDNthe2nd Jun 27 '18 at 23:59
  • 2
    This chap has a [smashing little answer](https://stackoverflow.com/questions/466345/converting-string-into-datetime) that will help get `string` -> `datetime` object. – Stefan Collier Jun 28 '18 at 00:12
  • Do you actually want to discard the 'T' separator and the milliseconds, or just because you don't know how to handle them? – smci Jun 28 '18 at 00:31

1 Answers1

3

For example:

import time
print time.strftime("%Y-%m-%d %H:%M:%S")
# 2018-06-28 08:00:35

As to your question:

import datetime
str_time = '2018-03-08T08:00:00.000'
d = datetime.datetime.strptime(str_time, "%Y-%m-%dT%H:%M:%S.%f")
print d
# 2018-03-08 08:00:00

New method:

import time

str_time = '2018-03-08T08:00:00.000'
d = time.strptime(str_time, "%Y-%m-%dT%H:%M:%S.%f")

print d
# time.struct_time(tm_year=2018, tm_mon=3, tm_mday=8, tm_hour=8, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=67, tm_isdst=-1)

print time.strftime("%Y-%m-%d %H:%M:%S", d)
# 2018-03-08 08:00:00
Jayhello
  • 5,931
  • 3
  • 49
  • 56
  • If I etc have `2018-03-08T08:00:00.000` this as my current time in the script let say `time = 2018-03-08T08:00:00.000` and I want to convert `time` to format of `2018-03-08 08:00:00` – CDNthe2nd Jun 28 '18 at 00:02
  • Oh I appreaciate it! Maybe this is abit overcourse now, but as you see the str_time is now lets act its a UK time- if I want to translate it to my time which is +1 hour from that time. Is there a way to make translate it to "my time" Not needed since I didn't ask for it so your question is correctly! – CDNthe2nd Jun 28 '18 at 00:10
  • @CDNthe2nd, I have update a new method, hope it helps. – Jayhello Jun 28 '18 at 00:22
  • Appreacite it alot @JayHello! Really helped me and you made a solution for it! Thanks! – CDNthe2nd Jun 28 '18 at 00:29
  • @CDNthe2nd: if you need to convert it to 'UTC+1' timezone, please edit your question to state that. – smci Jun 28 '18 at 00:30
  • Yeah. I mean its not necessary since that wasn't my question @smci :) Im happy with this, Its just a bonus with UTC+1 :) – CDNthe2nd Jun 28 '18 at 00:31
  • 1
    @CDNthe2nd: but please edit the question to be a clear and useful resource for other people who come here in future with the same problem. It's not just about solving your issue ;-) Welcome to SO – smci Jun 28 '18 at 00:33
  • Oh I mean I think I have done it correctly, @Jayhello did present the solution for me where he used the ISOformat and converted it to the format that I wished for (datetime) and he did that with using strpttime which was exactly what I wanted :) – CDNthe2nd Jun 28 '18 at 00:34