Sunday, 2 August 2015

Lets Play with Date in Python....

Getting Fromated Time


import time;
localtime = time.asctime( time.localtime(time.time()) )
print "Local current time :", localtime

Output
Local current time : Tue Jan 13 10:17:09 2009

Getting Calendar For A Month

#!/usr/bin/python
import calendar
cal = calendar.month(2008, 1)
print "Here is the calendar:"
print cal;

Output
Here is the calendar:
    January 2008
Mo Tu We Th Fr Sa Su
    1  2  3  4  5  6
 7  8  9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
              

How To Convert Date to DateTime

Example:

import datetime
import dateutil.relativedelta from datetime import date, timedelta date_from = obj.date_from//we are taking date(2014-6-11) date_to = obj.date_to//date formate date_from = datetime.datetime.strptime(obj.date_from, "%Y-%m-%d") date_to = datetime.datetime.strptime(obj.date_to, "%Y-%m-%d")

output
date_from = 2014-6-11 00:00:00



How Many Month Between Two Dates

1)first Convert date in to datetime objects(Refer previous) 2)Month = (12 * date_to.year + date_to.month) - (12 * date_from.year + date_from.month)




 

How To Print Current Date Time


from datetime import datetime
from dateutil.relativedelta import relativedelta
import time
from math import *

today_current_datetime = datetime.now().strftime('%Y-%m-%d %H:%M:%S')

today_current_datetime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') 

Finding Days, Hours,Minutes,seconds between Two Dates


from datetime import datetime
from dateutil.relativedelta import relativedelta
import time
from math import *

start_date = task.work_id.date (Normal Date)
end_date = datetime.now().strftime('%Y-%m-%d %H:%M:%S') (current datetime)
duration = datetime.strptime(end_date, '%Y-%m-%d %H:%M:%S') - datetime.strptime(start_date, '%Y-%m-%d %H:%M:%S')


days, seconds = duration.days, duration.seconds
hours = days * 24 + seconds // 3600
minutes = (seconds % 3600) // 60
seconds = seconds % 60
hours_toupdate = int(hours) + (float(minutes)/60)

How To Add One Day To The Date


from datetime import datetime,date
from datetime import timedelta

production_date_planned_from = datetime.strptime(production_date_planned,"%Y-%m-%d") // production_date_planned normal date
production_date_planned_to = production_date_planned_from + timedelta(days=1)



How To add Months to Date


import datetime
import dateutil.relativedelta
from datetime import date, timedelta

date_from = obj.date_from (normal date)
date_from_str =str(date_from)
d = datetime.datetime.strptime(date_from_str, "%Y-%m-%d")
new_date= d – dateutil.relativedelta.relativedelta(months=int(7))//substracting 7 months




How Can I convert Datetime to Date


from dateutil.relativedelta import relativedelta
import time
from math import *
from datetime import datetime

production_date_planned_to =2014-08-17 00:00:00

date = production_date_planned_to.strftime('%Y/%m/%d')
2014/08/17


How can I find Periods From Date


period_pool = self.pool.get('account.period')
search_periods = period_pool.find(cr, uid, slip.date_to, context=ctx)
period_id = search_periods[0]
period_from_date_value = period_from_date_value.with_context().find(date_value)[:1]
period_id = period_from_date_value.id





Year start date and end date


start_date = date(date.today().year, 1, 1) date
end_date = date(date.today().year, 12, 31)

year_start_date = str(datetime.strptime(str(date(date.today().year, 1, 1)), "%Y-%m-%d"))
year_end_date = str(datetime.strptime(str(date(date.today().year, 12, 31)), "%Y-%m-%d")) date time


Getting Year from Datetime


leave.date_from.split('-')[0] year
leave.date_from.split('-')[1] month

leave.date_from.split('-')[0] day

How to get Current Date


fields.date.today()


No comments:

Post a Comment