python 对时间操作

时间:2021-08-14 22:11:28

 

from datetime import datetime,timedelta

 

 

'date_test':fields.function(_datetime_all,type='datetime',  string=u'测试'),
函数:形式
def _datetime_all(self, cr, uid, ids, field_name, arg, context=None):

        res = {}
        for order in self.browse(cr, uid, ids, context=context):
            aa = order.lreport_date
            dt = datetime.strptime(aa, "%Y-%m-%d %H:%M:%S")
            d = dt-timedelta(hours=8)
            order.date_test=d
            va= order.date_test
            res[order.id] = va

        return  res



这个是on_change事件:
def on_change_time(self,cr,uid,ids,date,context=None):
        result={}
        if date:
            dt = datetime.strptime(date, "%Y-%m-%d %H:%M:%S")
            d = dt-timedelta(hours=8)
            result['date_test']=d
        return {'value':result}


 <field name="lreport_date"  on_change="on_change_time(lreport_date)"/>
 on_change="on_change_time(date)"

 

 

<script type="text/javascript">
    //替换字符串  
    function Replace(str, from, to) {
        return str.split(from).join(to);
    }
    // 日期类型格式成指定的字符串
    function FormatDate(date, format) {
        format = Replace(format, "yyyy", date.getFullYear());
        format = Replace(format, "MM", GetFullMonth(date));
        format = Replace(format, "dd", GetFullDate(date));
        format = Replace(format, "HH", GetFullHour(date));
        return format;
    }
    //js日期字符串转换成日期类型
    function parseDate(dateStr) {
        return new Date(Replace(dateStr, "-", "/"));
    }
    //增加月  
    function AddMonths(date, value) {
        date.setMonth(date.getMonth() + value);
        return date;
    }
    //增加天  
    function AddDays(date, value) {
        date.setDate(date.getDate() + value);
        return date;
    }
    //增加时
    function AddHours(date, value) {
        date.setHours(date.getHours() + value);
        return date;
    }
    //返回月份(两位数)  
    function GetFullMonth(date) {
        var v = date.getMonth() + 1;
        if (v > 9) return v.toString();
        return "0" + v;
    }
 
    //返回日(两位数)  
    function GetFullDate(date) {
        var v = date.getDate();
        if (v > 9) return v.toString();
        return "0" + v;
    }
    //返回时(两位数)
    function GetFullHour(date) {
        var v = date.getHours();
        if (v > 9) return v.toString();
        return "0" + v;
    }
    //比较两个时间
    function compareDate() {
        var mydate = AddDays(parseDate("2012-08-23"), 1);
        var nowdate = new Date();
        if (nowdate.getTime() < mydate.getTime()) {
            return FormatDate(nowdate, "yyyy-MM-dd");
        }
        return FormatDate(mydate, "yyyy-MM-dd");
    }
</script>