I am trying to get only the year output from the below script, but when I check for the same I am getting
/year=2020-08-20 19:11:26.616679/month=2020-08-20 19:11:26.616689/date=2020-08-20 19:11:26.616690'
Is there a way to get in year=2020, month=08, and date=20 format?
year = datetime.datetime.now()
from datetime import date
print(date.today().strftime('%Y, %m, %d'))
With datetime module, you can have something like this
from datetime import datetime
year = datetime.now()
print(datetime.strftime(year, 'year=%Y, month=%m, date=%d'))
Related
I have this column where the string has date, month, year and also time information. I need to take the date, month and year only.
There is no space in the string.
The string is on this format:
date
Tuesday,August22022-03:30PMWIB
Monday,July252022-09:33PMWIB
Friday,January82022-09:33PMWIB
and I expect to get:
date
2022-08-02
2022-07-25
2022-01-08
How can I get the date, month and year only and change the format into yyyy-mm-dd in python?
thanks in advance
Use strptime from datetime library
var = "Tuesday,August22022-03:30PMWIB"
date = var.split('-')[0]
formatted_date = datetime.strptime(date, "%A,%B%d%Y")
print(formatted_date.date()) #this will get your output
Output:
2022-08-02
You can use the standard datetime library
from datetime import datetime
dates = [
"Tuesday,August22022-03:30PMWIB",
"Monday,July252022-09:33PMWIB",
"Friday,January82022-09:33PMWIB"
]
for text in dates:
text = text.split(",")[1].split("-")[0]
dt = datetime.strptime(text, '%B%d%Y')
print(dt.strftime("%Y-%m-%d"))
An alternative/shorter way would be like this (if you want the other date parts):
for text in dates:
dt = datetime.strptime(text[:-3], '%A,%B%d%Y-%I:%M%p')
print(dt.strftime("%Y-%m-%d"))
The timezone part is tricky and works only for UTC, GMT and local.
You can read more about the format codes here.
strptime() only accepts certain values for %Z:
any value in time.tzname for your machine’s locale
the hard-coded values UTC and GMT
You can convert to datetime object then get string back.
from datetime import datetime
datetime_object = datetime.strptime('Tuesday,August22022-03:30PM', '%A,%B%d%Y-%I:%M%p')
s = datetime_object.strftime("%Y-%m-%d")
print(s)
You can use the datetime library to parse the date and print it in your format. In your examples the day might not be zero padded so I added that and then parsed the date.
import datetime
date = 'Tuesday,August22022-03:30PMWIB'
date = date.split('-')[0]
if not date[-6].isnumeric():
date = date[:-5] + "0" + date[-5:]
newdate = datetime.datetime.strptime(date, '%A,%B%d%Y').strftime('%Y-%m-%d')
print(newdate)
# prints 2022-08-02
I have been trying to convert a formatted string like
"1900-01-01" into a timestamp
datetime.strptime('1900-01-01','%Y-%m-%d').timestamp()
this works fine in linux/mac but in windows I got an error
OSError [Errno22] invalid argument I understand that windows don't support datetime before 1970
Would you please suggest a workround for this?
just use the total_seconds from a timedelta, which you obtain by subtracting the epoch:
from datetime import datetime
ts = (datetime.strptime('1900-01-01', '%Y-%m-%d') - datetime(1970, 1, 1)).total_seconds()
# ts
# -2208988800.0
Here is a possible way using regex and relativedelta
import re
from datetime import datetime
regex = re.match('(\d{4})-(\d{2})-(\d{2})','1900-01-01')
year, month, date = regex.groups()
year, month, date = int(year), int(month), int(date)
(datetime(year, month, date)-datetime(1970, 1, 1)).total_seconds()
This will give you -2208988800.0 which is the correct timestamp
I'a m trying to extract only time (Hour:Minute) from datetime field
Example:
today_with_hour = fields.Datetime(
string=u'hora',
default=fields.Datetime.now,
)
I would like to know how get only hour from today_with_hour in format
17:10:20
This is one way to extract:
from datetime import datetime
now = datetime.now()
print(str(now.hour)+':'+str(now.minute)+':'+str(now.second))
This may be better way to do it
You can use strftime
Example:
from datetime import datetime
datetime.now().strftime("%H:%M:%S")
In your case you can follow like this:
from datetime import datetime
datetime.strptime('20/06/2019 17:28:52', "%d/%m/%Y %H:%M:%S").time()
Output will be:
17:28:52
Better way to do is by using strftime().
dt = datetime.strptime('20/06/2019 17:28:52', "%d/%m/%Y %H:%M:%S")
dt.strftime("%H:%M:%S")
output:
17:28:52
To get the current time from the datetime.now()
datetime.datetime.now().time().strftime("%H:%M:%S")
O/P:
'11:16:17'
if you want to get the time with milliseconds also, use isoformat()
datetime.datetime.now().time().isoformat()
O/P:
'11:20:34.978272'
I'm trying to get the current timestamp and then its hour by doing the following:
from datetime import datetime
curr = datetime.today().strftime('%Y-%m-%d %H:%M:%S')
end_date = curr.split(':')[0]+':00:00'
I'm getting the output for end_date as follows: 2018-11-10 10:00:00. But this is currently in UTC time. How do I get it in PDT time?? Thanks!!
You can do this by importing the pytz module. Also, you can just generate the date format you need from the datetime object. So based on your code snippet, PDT time can be obtained like this.
from datetime import datetime
import pytz
curr = datetime.now(pytz.timezone('US/Pacific'))
full_date = curr.strftime('%Y-%m-%d %H:%M:%S')
hour_date = curr.strftime('%Y-%m-%d %H:00:00')
You will have to import pyzt module to get the time for the time zone that you want. Check this documentation
I am Python beginner. I have a question about Date and time code:
import datetime
date = datetime.date.today()
print(date.strftime('date is %d%b,%Y'))
Now I want to print the date of my own choice. How can I do it?
For example, if today's date is (15 sep 2016) and I want to print (23 oct 1998), what should I do in order to make it work?
You may do it like so:
>>> from datetime import date
>>> date(year=1998, month=10, day=23)
datetime.date(1998, 10, 23)
>>> _.strftime('date is %d %b,%Y')
'date is 23 Oct,1998'
As per datetime.date() documentation, date() accepts a year, month and a day.
You can initialize date using datetime constructor.
>>>import datetime
>>>d = datetime.datetime(1998,10,23)
>>>print(d.strftime('date is %d %b,%Y'))
If you needed to take a string value as input you can use
from dateutil import parser
datestring = "23 Oct 1998"
customDate = parser.parse(datestring)
print(customDate.strftime('date is %d%b,%y'))