Hi everyone I have a code like this to calculate the exact day on six_months back but unfortunately it prints the yy-mm-dd format and I want the dd/mm/yy format how do I do it(I tried to convert but it doesn't work)?What's wrong with my code?
import datetime
six_months = str(datetime.date.today() - datetime.timedelta(6*365/12-1)
datetime.datetime.strptime(six_months, '%Y-%m-%d').strftime('%d/%m/%y')
expected output=04/02/2017
current output=2017-02-04
The code is fine, you just forgot to save to result of
datetime.datetime.strptime(six_months, '%Y-%m-%d').strftime('%d/%m/%y')
to any variable. strptime doesn't change the object it is called on in any way, it returns a string.
I reckon your algorithm for computing 6 months back doesn't correspond to the real-world understanding of that phrase. Six months back from 4 August is 4 February and your computation gives the right answer for that. But six months back from 4 September is 4 March, and your computation gives the answer 7 March.
Your code also unnecessarily formats the computed date to a string, and then has to parse the string back into a date to get the dd/mm/yy format you want.
import datetime
from dateutil.relativedelta import *
six_months = datetime.date.today() + relativedelta(months=-6)
print (f"{six_months:%d/%m/%y}")
Output (until tomorrow) is
04/02/17
Related
I have the program that generate datetime in several format like below.
1 day, 21:21:00.561566
11:19:26.056148
Maybe it have in month or year format, and i want to know are there any way to plus these all time that i get from the program.
- 1 day, 21:21:00.561566 is the string representation of a datetime.timedelta object. If you need to parse from string to timedelta, pandas has a suitable method. There are other third party parsers; I'm just using this one since pandas is quite common.
import pandas as pd
td = pd.to_timedelta('- 11:19:26.056148')
# Timedelta('-1 days +12:40:33.943852')
td.total_seconds()
# -40766.056148
If you need to find the sum of multiple timedelta values, you can sum up their total_seconds and convert them back to timedelta:
td_strings = ['- 1 day, 21:21:00.561566', '- 11:19:26.056148']
td_sum = pd.Timedelta(seconds=sum([pd.to_timedelta(s).total_seconds() for s in td_strings]))
td_sum
# Timedelta('-1 days +10:01:34.505418')
...or leverage some tools from the Python standard lib:
from functools import reduce
from operator import add
td_sum = reduce(add, map(pd.to_timedelta, td_strings))
# Timedelta('-1 days +10:01:34.505418')
td_sum.total_seconds()
# -50305.494582
You can subtract date time like here to find how far apart these two times are:
https://stackoverflow.com/a/1345852/2415706
Adding two dates doesn't really make any sense though. Like, if you try to add Jan 1st of 2020 to Jan 1st of 1995, what are you expecting?
You can use datatime.timedelta class for this purpose.
You can find the documentation here.
You will need to parse your string and build a timedelta object.
I need to parse a few dates that are roughly in the format (1 or 2-digit year)-(Month abbreviation), for example:
5-Jun (June 2005)
13-Jan (January 2013)
I tried using strptime with the format %b-%y but it did not consistently produce the desired date. Per the documentation, this is because some years in my dataset are not zero-padded.
Further, when I tested the datetime module (please see below for my code) on the string "5-Jun", I got "2019-06-05", instead of the desired result (June 2005), even if I set yearfirst=True when calling parse.
from dateutil.parser import parse
parsed = parse("5-Jun",yearfirst=True)
print(parsed)
It will be easier if 0 is padded to single digit years, as it can be directly converted to time using format. Regular expression is used here to replace any instance of single digit number with it's '0 padded in front' value. I've used regex from here.
Sample code:
import re
match_condn = r'\b([0-9])\b'
replace_str = r'0\1'
datetime.strptime(re.sub(match_condn, replace_str, '15-Jun'), '%y-%b').strftime("%B %Y")
Output:
June 2015
One approach is to use str.zfill
Ex:
import datetime
d = ["5-Jun", "13-Jan"]
for date in d:
date, month = date.split("-")
date = date.zfill(2)
print(datetime.datetime.strptime(date+"-"+month, "%y-%b").strftime("%B %Y"))
Output:
June 2005
January 2013
Ah. I see from #Rakesh's answer what your data is about. I thought you needed to parse the full name of the month. So you had your two terms %b and %y backwards, but then you had the problem with the single-digit years. I get it now. Here's a much simpler way to get what you want if you can assume your dates are always in one of the two formats you indicate:
inp = "5-Jun"
t = time.strptime(("0" + inp)[-6:], "%y-%b")
I have exported a list of AD Users out of AD and need to validate their login times.
The output from the powershell script give lastlogin as LDAP/FILE time
EXAMPLE 130305048577611542
I am having trouble converting this to readable time in pandas
Im using the following code:
df['date of login'] = pd.to_datetime(df['FileTime'], unit='ns')
The column FileTime contains time formatted like the EXAMPLE above.
Im getting the following output in my new column date of login
EXAMPLE 1974-02-17 03:50:48.577611542
I know this is being parsed incorrectly as when i input this date time on a online converter i get this output
EXAMPLE:
Epoch/Unix time: 1386031258
GMT: Tuesday, December 3, 2013 12:40:58 AM
Your time zone: Monday, December 2, 2013 4:40:58 PM GMT-08:00
Anyone have an idea of what occuring here why are all my dates in the 1970'
I know this answer is very late to the party, but for anyone else looking in the future.
The 18-digit Active Directory timestamps (LDAP), also named 'Windows NT time format','Win32 FILETIME or SYSTEMTIME' or NTFS file time. These are used in Microsoft Active Directory for pwdLastSet, accountExpires, LastLogon, LastLogonTimestamp and LastPwdSet. The timestamp is the number of 100-nanoseconds intervals (1 nanosecond = one billionth of a second) since Jan 1, 1601 UTC.
Therefore, 130305048577611542 does indeed relate to December 3, 2013.
When putting this value through the date time function in Python, it is truncating the value to nine digits. Therefore the timestamp becomes 130305048 and goes from 1.1.1970 which does result in a 1974 date!
In order to get the correct Unix timestamp you need to do:
(130305048577611542 / 10000000) - 11644473600
Here's a solution I did in Python that worked well for me:
import datetime
def ad_timestamp(timestamp):
if timestamp != 0:
return datetime.datetime(1601, 1, 1) + datetime.timedelta(seconds=timestamp/10000000)
return np.nan
So then if you need to convert a Pandas column:
df.lastLogonTimestamp = df.lastLogonTimestamp.fillna(0).apply(ad_timestamp)
Note: I needed to use fillna before using apply. Also, since I filled with 0's, I checked for that in the conversion function about, if timestamp != 0. Hope that makes sense. It's extra stuff but you may need it to convert the column in question.
I've been stuck on this for couple of days. But now i am ready to share really working solution in more easy to use form:
import datetime
timestamp = 132375402928051110
value = datetime.datetime (1601, 1, 1) +
datetime.timedelta(seconds=timestamp/10000000) ### combine str 3 and 4
print(value.strftime('%Y-%m-%d %H:%M:%S'))
This question already has answers here:
Convert weird Python date format to readable date
(2 answers)
Closed 7 years ago.
Recently I received this output from an API (I think it's .NET driven).. What kind of date format is this and how do I convert it to a Python date object?
{
Id: 10900001,
ExpirationDate: "/Date(1262476800000)/",
}
It seems a timestamp but I get parse errors on fromtimestamp()
>>> from datetime import datetime
>>> datetime.fromtimestamp(float('1262476800000'))
ValueError: 'year is out of range'
You are getting the error because the timestamp is in milliseconds. You just remove the last 3 digits and it will work -
>>> from datetime import datetime, timedelta
>>> s = '1262476800540'
>>> d = datetime.fromtimestamp(float(s[:-3]))
>>> d = d + timedelta(milliseconds=int(s[-3:]))
>>> print d
2010-01-03 05:30:00.540000
>>> from datetime import datetime
>>> a = datetime.fromtimestamp(float('1262476800002')/1000)
>>> a.microsecond
2000
As it seems the API's output is JSON, I would assume it is a javascript timestamp. To convert it to python, remove the milliseconds and you should be fine.
From an online conversion tool http://www.epochconverter.com/
GMT: Sun, 03 Jan 2010 00:00:00 GMT
It seems like a UNIX time stamp in milliseconds. In other words 1262476800(000) = 2010-01-03T00:00:00+00:00
Could that be correct?
You can use fromtimestamp to convert it to a date object.
Cheers,
Anders
Also you can use time module.
In [13]: import time
In [14]: time.ctime(1262476800000)
Out[14]: 'Mon Apr 19 02:00:00 41976\n'
I think you'll have to peruse the documentation of that API. Failing which, can you reverse-engineer it? If you can create entities in that system, then do so. If you create 3 entities expiring on 1st Jan 2016, 11th Jan 2016 and 21st Jan 2016, you'll be able to see if it's (probably) a linearly increasing sequence of time-units and deduce what are the units and the base-date. Even if you can't create entities, can you get the dates in human-readable format through a human-orientated web interface?
Once you know what the number represents, you can decode it either into year, month, day ... fields, or into a number of seconds since the canonical base date (timestamp).
Or maybe you'll get lucky and another reader will recognise the format!
I had never worked with the datetime module in Python 2.3, and I have a very silly problem. I need to read a date in the format
'10-JUL-2010'
then subtract a day (I would use timedelta), and return the string
'09-JUL-2010 00:00:00 ET'
of course, this is for hundreds of dates. While it should be trivial, I cannot find the info on how to read formatted dates in Python 2.3! Help!
Edit
I am able to retrieve the formatted date as a tuple, but it will not accept the timedelta object for subtraction! Still working on it...
** Edit **
Finally... thanks to your help I was able to solve the problem as follows:
print (datetime(*(time.strptime(date_string, format)[0:6])).strftime('%d-%b-%Y')).upper()+'00:00:00 ET'
You're looking for datetime.datetime.strptime(), but the documentation is awful for that function, it's effectively the reverse operation of datetime.datetime.strftime().
The format string you're looking for is: '%d-%b-%Y'
See: http://www.python.org/doc/2.3.5/lib/node211.html and http://www.python.org/doc/2.3.5/lib/datetime-datetime.html and http://www.python.org/doc/2.3.5/lib/module-time.html
Edit: Oh snap! There is no strptime in the datetime module in python 2.3. It's in the time module, you'll have to use that one instead.
Well, there is no builtin for that in 2.3, only from 2.5 on. But for that one format you can parse it by hand ...
months = { 'JAN' : 1, 'FEB' : 2, ... } # write that yourself :p
day,mon,year = thedate.split('-')
day = int(day)
mon = months[mon]
year = int(year)
parsed = datetime.datetime(day=day, month=month, year=year)
If strptime() is not working out for you there is also the option of brute forcing it with a regex:
import re
import date
timestamp_regex = re.compile(r"(\d\d)-(\w\w\w)-(\d\d\d\d)")
# month_mapping: a mapping for 3 letter months to integers
d1 = datetime.date(int(match.group(3)), #year
month_mapping[match.group(2)],#month
int(match.group(1))) #day