How do I parse a Microsoft JSON date in Python? - python

I have to read a JSON file and It has the date fields formated like this below
"MyDate": "/Date(1603284014088)/",
I found some examples to parse it in other languages (JavaScript, Kotlin) but nothing in Python.
I could find on this SCOTT HANSELMAN's post here that they are milliseconds since the beginning of the Unix Epoch sometimes with a TimeZone. So I could write a simple function to decode it.
import re
from datetime import datetime, timedelta
def decode_date (encoded_date):
mask = "\/Date\(([0-9]*)\)"
offset = datetime(1970, 1, 1)
my_matchs = re.match(mask, encoded_date).groups()
if len(my_matchs) == 1:
return datetime(1970, 1, 1) + timedelta(milliseconds=int(my_matchs[0]))
else:
return None
encoded = "/Date(1603284014088)/"
print (decode_date(encoded))
This function can't parse dates with timezone because I'm lazy :), My question is - is there some lib that can parse it out of the box in Python?

Given the fact that you are already able to extract the date using RegEx please try the following code for conversion:
import datetime
s = '1603284014088'
fmt = "%Y-%m-%d %H:%M:%S"
# Local time
t = datetime.datetime.fromtimestamp(float(s)/1000.)
print (t.strftime(fmt))
# UTC time
t_utc = datetime.datetime.utcfromtimestamp(float(s)/1000.)
print (t_utc.strftime(fmt))

Related

Split URL at - With Python

Does anyone know how I can extract the end 6 characters in a absoloute URL e.g
/es/ideas-de-trading-y-noticias/el-ibex-35-insiste-en-buscar-los-7900-puntos-a-la-espera-de-las--221104
This is not a typical URL sometimetimes it ends -221104
Also, is there a way to turn 221104 into the date 04 11 2022 easily?
Thanks in advance
Mark
You should use the datetime module for parsing strings into datetimes, like so.
from datetime import datetime
url = 'https://www.ig.com/es/ideas-de-trading-y-noticias/el-ibex-35-insiste-en-buscar-los-7900-puntos-a-la-espera-de-las--221104'
datetime_string = url.split('--')[1]
date = datetime.strptime(datetime_string, '%y%m%d')
print(f"{date.day} {date.month} {date.year}")
the %y%m%d text tells the strptime method that the string of '221104' is formatted in the way that the first two letters are the year, the next two are the month, and the final two are the day.
Here is a link to the documentation on using this method:
https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior
If the url always has this structure (that is it has the date at the end after a -- and only has -- once), you can get the date with:
str_date = str(url).split("--")[1]
Relaxing the assumption to have only one --, we can have the code working by just taking the last element of the splitted list (again assuming the date is always at the end):
str_date = str(url).split("--")[-1]
(Thanks to #The Myth for pointing that out)
To convert the obtained date into a datetime.date object and get it in the format you want:
from datetime import datetime
datetime_date = datetime.strptime(str_date, "%y%m%d")
formatted_date = datetime_date.strftime("%d %m %Y")
print(formatted_date) # 04 11 2022
Docs:
strftime
strptime
behaviour of the above two functions and format codes
Taking into consideration the date is constant in the format yy-mm-dd. You can split the URL by:
url = "https://www.ig.com/es/ideas-de-trading-y-noticias/el-ibex-35-insiste-en-buscar-los-7900-puntos-a-la-espera-de-las--221104"
time = url[-6:] # Gets last 6 values
To convert yy-mm-dd into dd mm yy we will use the DateTime module:
import datetime as dt
new_time = dt.datetime.strptime(time, '%y%m%d') # Converts your date into datetime using the format
format_time = dt.datetime.strftime(new_time, '%d-%m-%Y') # Format
print(format_time)
The whole code looks like this:
url = "https://www.ig.com/es/ideas-de-trading-y-noticias/el-ibex-35-insiste-en-buscar-los-7900-puntos-a-la-espera-de-las--221104"
time = url[-6:] # Gets last 6 values
import datetime as dt
new_time = dt.datetime.strptime(time, '%y%m%d') # Converts your date into datetime using the format
format_time = dt.datetime.strftime(new_time, '%d %m %Y') # Format
print(format_time)
Learn more about datetime
You can use python built-in split function.
date = url.split("--")[1]
It gives us 221104
then you can modify the string by rearranging it
date_string = f"{date[4:6]} {date[2:4]} {date[0:2]}"
this gives us 04 11 22
Assuming that -- will only be there as it is in the url you posted, you can do something as follows:
You can split the URL at -- & extract the element
a = 'https://www.ig.com/es/ideas-de-trading-y-noticias/el-ibex-35-insiste-en-buscar-los-7900-puntos-a-la-espera-de-las--221104'
desired_value = a.split('--')[1]
& to convert:
from datetime import datetime
converted_date = datetime.strptime(desired_value , "%y%m%d")
formatted_date = datetime.strftime(converted_date, "%d %m %Y")

How to extract date from filename in python?

I need to extract the event date written on the filename to be in a new column called event_date, I am assumed I can use regex but I still do not get the exact formula to implement.
The filename is written below
file_name = X-Y Cable Installment Monitoring (10-7-20).xlsx
The (10-7-20) is in mm-dd-yy format.
I expect the date would result df['event_date'] = 2020-10-07
How should I write my script to get the correct date from the filename.
Thanks in advance.
use str.rsplit() with datetime module -
Steps -
extract date
convert it into the required datetime format.
from datetime import datetime
file_name = 'X-Y Cable Installment Monitoring (10-7-20).xlsx'
date = file_name.rsplit('(')[1].rsplit(')')[0] # '10-7-20'
date = datetime.strptime(date, "%m-%d-%y").strftime('%Y-%m-%d') # '2020-10-07'
Or via regex -
import re
regex = re.compile(r"(\d{1,2}-\d{1,2}-\d{2})") # pattern to capture date
matchArray = regex.findall(file_name)
date = matchArray[0]
date = datetime.strptime(date, "%m-%d-%y").strftime('%Y-%m-%d')

Unable to convert a string to a datetime object in python and perform some time related calculations with it

What I am trying to do is to get the current datetime of a place using an API with python and extract datetime from it. The code that I have is:
import requests
import json
from datetime import datetime
import time
def get_current_time_from_api():
response = (requests.get("http://worldtimeapi.org/api/ip")).json()
return response["datetime"]
the_time = get_current_time_from_api()
When I print the response using print(the_time), the response that gets returned is:
2020-05-06T10:04:51.368291+05:45
Then, I tried getting the converting the string to datetime using the function datetime.strptime(the_time, "%X) to get the time, I get the error ValueError: time data '2020-05-06T10:09:52.009222+05:45' does not match format '%X' So, what went wrong and how can I do things like this when the time is parsed from the string?
if(time == "10:00:00 pm"):
#do something here
else:
difference_in_minutes = "10:00:00" - current_time
time.sleep(difference_in_minutes * 100) #sleeping for that many seconds
#do stuff when the time is 10 pm
I think you may be looking for the fromisoformat method. Try this:
import datetime as dt
dt.datetime.fromisoformat(the_time).strftime('%X')
Output:
'21:37:54'
from datetime import datetime
from dateutil.relativedelta import relativedelta
datetime_obj = datetime.strptime(
'2020-05-06T10:04:51.368291+05:45', # your datetime string
'%Y-%m-%dT%H:%M:%S.%f%z' # format of datetime
)
# this is how you can add a day to date_object
new_date = datetime_obj + relativedelta(days=1)
This is wrong time == "10:00:00 pm"
you should use datetime_objects to compare, like:
if new_date > datetime_obj: # this is true.
# You can do things here
print("Yes")
# if(time == "10:00:00 pm"), should be done like this:
if datetime_obj.time().hour == 10 and datetime_obj.time().min == 10:
pass
See for datetime formatting :
https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes
relativedelta:
https://dateutil.readthedocs.io/en/stable/relativedelta.html

problem of date conversion from english to french

I try to convert a date in english (2019-10-07) in french (07/10/2016)
I try
dat = '07/10/2019'
dat = time.strftime('%Y-%m-%d')
but got the result '2019-10-16' instead of '2019-10-07'
using datetime you can decide the format in which the source date is provided, and the target format you want.
from datetime import datetime
dat = '07/10/2019'
datetime.strptime(dat, "%d/%m/%Y").strftime("%Y-%m-%d")
out[6]: '2019-10-07'
strftime needs a time/date to convert, and it will use the current date and time if you don't provide one. The previous value of dat is not relevant - this information is not seen by strftime.
You need to provide the time information that strftime will format, as a tuple that you can get by parsing the original string. For this, use strptime (f for format, p for parse).
So:
dmy = '07/10/2019'
ymd = time.strftime('%Y-%m-%d', time.strptime(dmy, '%d/%m/%Y'))
# ^^^^^^^^ ^^^^^^^^
# output schema input schema
# now ymd is '2019-10-07'
(Or you can use the datetime module as in the other answer. This way, the parsing gives you an object, which has a method to format back - so you can write the whole operation "in order" on the line. But the general principle is the same: you need to parse, then format, and you need to specify the schema on each side.)
with :
dat = time.strftime('%Y-%m-%d')
you recover your actual date.
you need to make :
from datetime import datetime
dat = '07/10/2019'
dat = datetime.strptime(dat, '%m/%d/%Y')
print(dat.strftime('%Y-%m-%d') )

How do I generate a python timestamp to a particular format?

In python, how would I generate a timestamp to this specific format?
2010-03-20T10:33:22-07
I've searched high and low, but I couldn't find the correct term that describes generating this specific format.
See the following example:
import datetime
now = datetime.datetime.now()
now.strftime('%Y-%m-%dT%H:%M:%S') + ('-%02d' % (now.microsecond / 10000))
This could result in the following:
'2017-09-20T11:52:32-98'
You can use datetime with strftime. Exemple:
import datetime
date = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f")
print(date)
Will print:
2017-09-20T12:59:43.888955

Categories

Resources