I have to split a date time in the below format to separate variables (date, time)
2015-11-31T13:45:00.000
Note: The whole date and time is stored in a single string variable.
I have to split a date time in the below format to separate variables (date, time)
2015-11-31T13:45:00.000
Note: The whole date and time is stored in a single string variable.
Always use a date library, to avoid the many errors that can happen when you try to do it yourself. In this case, it looks like November the 31st, which is not a valid day.
https://docs.python.org/3/library/datetime.html#datetime.datetime.strptime
Of course, you'll have to help it with the format. We're just going to ignore the T, by making it a literal in the format string. See What exactly does the T and Z mean in timestamp?
import datetime
format_string = "%Y-%m-%dT%H:%M:%S.%f"
date_string = "2015-11-31T13:45:00.000"
datetime.datetime.strptime(date_string, format_string)
builtins.ValueError: day is out of range for month
Once you have the datetime, it will be relatively easy to print out whatever components you want with strftime. Of course, you could just split on 'T' if that literal is always there, but then you wouldn't really have reliable time objects. It wouldn't tell you that Nov 31 is not a valid day.