oharr
oharr

Reputation: 183

Python converting a string salesforce timestamp to date

This is the datetime string I am getting 2018-05-04T13:08:20.000+0000. I want to convert it to a date 2018-05-04. Below is the for loop i want use but just can't seem to strip out all the hour secs and etc. Max_data is the string with 2018-05-04T13:08:20.000+0000

from datetime import timedelta, date

def daterange(start_date, end_date):
    for n in range(int ((end_date - start_date).days)):
        yield start_date + timedelta(n)

start_date = datetime.strptime(max_data,'%Y-%m-%d')
end_date = date.today()
for single_date in daterange(start_date, end_date):
    print single_date.strftime("%Y-%m-%d")

Upvotes: 1

Views: 2824

Answers (4)

AChampion
AChampion

Reputation: 30258

Just parse the whole date string and then just use what you need, e.g.:

In []:
from datetime import datetime, timedelta

def daterange(start_date, end_date):
    for n in range(int ((end_date - start_date).days)):
        yield start_date + timedelta(n)

start_date = datetime.strptime(max_data,"%Y-%m-%dT%H:%M:%S.%f%z")
end_date = datetime.now(start_date.tzinfo)
for single_date in daterange(start_date, end_date):
    print(single_date.strftime("%Y-%m-%d"))

Out[]:
2018-05-04
2018-05-05
2018-05-06
2018-05-07
2018-05-08
2018-05-09

Note: you need to pass in the tzinfo to now() to get a tz aware datetime object (instead of using date.today())

Note: in Py3.7 the standard library has added, datetime.fromisoformat().

Upvotes: 0

pstatix
pstatix

Reputation: 3848

Easy to do in a one liner:

from datetime import datetime as dt

max_data =  '2018-05-04T13:08:20.000+0000'
date = dt.strptime(max_data.split('T')[0], '%Y-%m-%d')
print (date)
# output: datetime.datetime(2018, 5, 4, 0, 0)

Upvotes: 0

Sudheesh Singanamalla
Sudheesh Singanamalla

Reputation: 2297

The timestamp string you're having is an ISO8601 string. You can parse the information of this string using the dateutil.parser module as follows:

import dateutil.parser
Max_data = "2018-05-04T13:08:20.000+0000"
converted_data = dateutil.parser.parse(Max_data)
date_format_string = converted_data.strftime("%Y-%m-%d")

The result of date_format_string is '2018-05-04'

Upvotes: 2

NaruS
NaruS

Reputation: 168

Using the information you gave, I did the following with my version of your code(Just the parts that you need to have fixed)

Max_data =  '2018-05-04T13:08:20.000+0000' #How ever max data is assigned, this is not needed

print Max_data[:10] # This will only print the 2018-05-04 while removing everything else

The output: 2018-05-04

Using Max_data[:10] is what it seems you are in need of(At least one method. It isn't the same as the original code so you may not like this fix, though it is shorter then the original code.

Remove the datetime.strptime(max_data,'%Y-%m-%d') and replace it withMax_data[:10]

I know you don't need it exactly the way I placed it but its just to show how to get rid of the information you don't need

Upvotes: 0

Related Questions