I'm really new to programming and I only just started using Python, if anyone could edit the code I put up to make it work how I want it to then please do.
I was wondering if I could make the date show up, on my python program but make it different for different regions, so if someone opened the program in United Kingdom the day would show up first and if it was in the US it would show the month or year first etc.
This is what I have so far.
import datetime
currentDate = datetime.date.today()
print(currentDate.strftime('Todays date is: %d %B %Y'))
I currently have it set so it shows the day first then the month then the year.
Is there anyway to make it use it in a different order depending on what country you're in?
Does this work for you?
>>> import datetime
>>> today = datetime.date.today()
>>> print(today.strftime('%x'))
09/10/15
Specifically, you probably should look at the %c, %x, and %X format codes. See 8.1.8. strftime() and strptime() Behavior for more information on how to use the strftime method.
Related
I have a question based on the conversion of a date and time using the start_time element's Google Calendar API, how could I convert it to a day, month and year of Latin America without more, if you could help me please
the datetime:
2021-01-19T09:00:00-05:00
I need the follow example: day/mount/year 15/01/2021
I've been trying and can't find a way to convert properly, please if you could help me.
Use the datetime library:
import datetime
date_str = "2021-01-19T09:00:00-05:00"
date_obj = datetime.datetime.strptime(date_str[:10], "%Y-%m-%d").strftime("%d/%m/%Y")
print(date_obj)
I am using python in scrapy and collecting a bunch of dates that are stored on a web page in the form of text strings like "11th November" (no year is provided).
I was trying to use
startdate = '11th November'
datetime.strptime(startdate, '%d %B')
but I don't think it likes the 'th' and I get a
Value error: time data '11th November' does not match format '%d %B'
If I make a function to try to strip out the th, st, rd, nd from the days I figured it will strip out the same text from the month.
Is there a better way to approach turning this into a date format?
For my use, it ultimately needs to be in the ISO 8601 format YYYY-MM-DD
This is so that I can pipe it from scrapy to a database, and from that use it in a Google Spreadsheet for a javascript Google chart. I just mention this because there may be a better place to make the string-to-date change than trying to do it in python.
(As a secondary issue, I also need to figure how to add the right year to the date given that if it says 12th January that would mean Jan 2020 and not 2019. This will be based on a comparison to the date when the scrape runs. i.e. the date today.)
EDIT:
it turned out that the solution required the secondary issue to be addressed as well. Hence the choice of final answer to this question. If the secondary issue of the year was not addressed it defaulted to 1900 which was a problem.
Try this out -
import datetime
datetime_obj = datetime.datetime.strptime(re.sub(r"\b([0123]?[0-9])(st|th|nd|rd)\b",r"\1", startdate) + " " + str(datetime.datetime.now().year), "%d %B %Y")
I'm developing a small program for an insurance company to see how old a person is (this changes the ranges of cover we can offer). I can easily calculate the different in the years but it does not take into account the days/months. I there any way of doing this without importing things from elsewhere?
as you are working with python you must use the datetime module. Lets have a look.
from datetime import datetime
bday = '1978/11/21'
dt = datetime.strptime(bday, '%Y/%m/%d')
diff = datetime.now() - dt
diff.days
Gives you 13764
import time;
localtime = time.asctime( time.localtime(time.time()) )
print "Local current time :", localtime
I'm trying to figure out how to add write a command like this:
"Next day." That will then print out the next day in the calendar and so forth.
I've managed to get the calendar in, but I'm in need of help on how to connect commands to it. Thanks in advance!
Writing a program that will handle a limited set of expressions entered by the user of the form "Next day" isn't that hard, but if you want to handle arbitrary date query expressions, things get a little complicated. :)
But if you just want to know how to manipulate dates (and times) in Python you will have to read the documentation for the datetime and calendar modules. The datetime module is rather large and a little bit messy, so don't expect to master it immediately. But if you read through the docs and write lots of little test programs you will soon learn how to use it.
To get you started, here's a small example which shows how to add or subtract an arbitrary number of days from a given date. To display the dates this program uses the strftime method, which you've probably already seen in the time module docs.
#!/usr/bin/env python
import datetime
def date_string(date):
return date.strftime('%A %d %B %Y')
oneday = datetime.timedelta(days=1)
today = datetime.date.today()
print today
print 'Today is', date_string(today)
print 'Tomorrow is', date_string(today + oneday)
print 'Yesterday was', date_string(today - oneday)
print 'In one week it will be', date_string(today + oneday * 7)
output
2015-02-24
Today is Tuesday 24 February 2015
Tomorrow is Wednesday 25 February 2015
Yesterday was Monday 23 February 2015
In one week it will be Tuesday 03 March 2015
I am new to python. I am looking for ways to extract/tag the date & time specific information from text
e.g.
1.I will meet you tomorrow
2. I had sent it two weeks back
3. Waiting for you last half an hour
I had found timex from nltk_contrib, however found couple of problems with it
https://code.google.com/p/nltk/source/browse/trunk/nltk_contrib/nltk_contrib/timex.py
b. Not sure of the Date data type passed to ground(tagged_text, base_date)
c. It deals only with date i.e. granularity at day level. Cant find expression like next one hour etc.
Thank you for your help
b) The data type that you need to pass to ground(tagged_text, base_date) is an instance of the datetime.date class which you'd initialize using something like:
from datetime import date
base_date = date.today()