I'm trying to work on a homework problem where I have an input date in the format YYYY-MM-DD
I need to import a couple modules and create a function weekday where it splits up the date and returns the weekday.
So far I imported:
from time import *
from datetime import *
I need help in my weekday function where I must use a .split method
Create an object of the class datetime.date.
and use strftime to return the day of the week in full text.
Can anyone help me get started on how to implement these functions and modules properly?
Suppose s is your input string:
s = '2010-10-29'
At the prompt, try s.split('-'). What happens?
Create a date object:
d = datetime.date(2010, 10, 29)
Then run d.strftime('%B'). What happens? How would you get the weekday, instead?
You can fill in the rest. Look at the Python docs for more information. Python doc: time, datetime
it's two lines
import time
print time.strftime("%A", time.strptime('2010-10-29', "%Y-%m-%d"))
prints 'Friday'
Related
I would like to print the date format so I dont need to search in the navigator every time I want to print a date like in the following code:
time = now.strftime("%Y-%m-%d %H:%M:%S")
print("time:", time)
I been searching and didn't find any thing about this.
When you run help(the_date.strftime), it doesn't show the possible parameters.
You can see the format codes here - Basic date and time types
The below sample reference:
import time
print("time:{}".format(time.strftime('%Y-%m-%d %H:%M:%S')))
I have a big .csv file which holds machine log data. One of the fields is timestamp. It stores date and time as shown in the title and I would like to drop the milli seconds and convert it into the format also shown in title. Can anyone help me with that? Im new to Python and Ipython.
Much obliged.
For your special case, this should suffice:
t.replace('T', ' ')[:19]
But I would recommend, that you use the datetime module of the standard library instead, so your time conversion also could be internationalized.
You can use easy_date to make it easy:
import date_converter
new_date = date_converter.string_to_string("2013-01-06T22:25:08.733", "%Y-%m-%dT%H:%M:%S.%f", "%Y-%m-%d %H:%M:%S")
For example I give the date as:
2/12/2015
The result should be:
February/Thursday/2015
I tried to do with if but I'm not getting the result. It would be nice if you could tell me the long way (without using built in functions (like datetime and others) too much). I'm new to python and not much is taught in my school.
You don't have to use datetime too much, simply parse the date and output it in whatever format you want
from datetime import datetime
d = "2/12/2015"
print(datetime.strptime(d,"%m/%d/%Y").strftime("%B/%A/%Y"))
February/Thursday/2015
A = Locale’s full weekday name.
B = Locale’s full month name.
Y = Year with century as a decimal number.
All the format directives are here
You could create a dict mapping but you will find datetime is lot simpler.
I am not sure I worded that correctly but python and time always confuses me.
This is what I am trying.
Given a unix timestamp (INT) which is definitely in the past (can be seconds ago or years ago) I want to generate a babel format_timedelta
My problem is
Babel format_timedelta takes timedelta as first argument
so I guess I need to generate a timedelta using time.time() (now) and the unix timestamp I have.
I can't figure out the part 2 and I believe there must be an easier/correct way to do this. Please share the best possible way to do this, also I need it to be fast in calculating since I have to use it in a web page.
def format_starttime(value, granularity="day"):
delta = datetime.timedelta(seconds=time.time() - value)
return format_timedelta(delta, granularity)
gives error in date.format_timedelta()
AttributeError: 'module' object has no attribute 'format_timedelta'
import datetime
td = datetime.timedelta(seconds=time.time()-a_unix_timestamp)
Difference between two datetime instances is a timedelta instance.
from datetime import datetime
from babel.dates import format_timedelta
delta = datetime.now() - datetime.fromtimestamp(your_timestamp)
print format_timedelta(delta, locale='en_US')
See datetime module documentation for details and more examples.
i have timestamps in the following format:
2011 February 2nd 13h 27min 21s
110202 132721
I want to convert 110202 132721 into the corresponding linux timestamp: 1296682041
Is there any quick efficient way to achieve this?
Something like
>>> s = "110202 132721"
>>> print time.mktime(time.strptime(s, "%y%m%d %H%M%S"))
1296653241.0
This interprets the time as a local time (your current time zone).
To create a Unix timestamp, use the time.mktime(t) function. It takes a time.struct_time object.
The objects definition can be viewed here. So you just have to parse the date and the time and put it into the object before handing it over to the mktime() function
Without your timezone information, this is not the 'corresponding' unix timestamp.
After a few attempts I have guessed you could be located in the Pacific coast of USA, so you have to define it explicitely in your script:
from datetime import datetime
import pytz
import calendar
a = "110202 132721"
yourTZ = 'America/Los_Angeles'
calendar.timegm(pytz.timezone(yourTZ).localize(datetime.strptime(a, '%y%m%d %H%M%S')).utctimetuple())
# returns 1296682041