1

I am matching two attributes in assembla(no DB) but one attribute save the date and time in GMT due to which I am unable to find the exact matches. I have decided to add +5 to the one which contains GMT time at runtime so the both the attributes date time will be same and I can find the exact match.

First I want to add the time so I can get the correct time, and then I am splitting the time because the other attribute doesn't contain time.

 abc_date = ticket['abc_date'].?
 abc_date = ticket['abc_date'].split('T')[0]

abc_date: 2015/4/1 8pm Is currently like this

abc_date: 2015/4/2 1am I want it like this after hours added

N.B abc_date is a datetime field.

TLS
  • 3,585
  • 1
  • 19
  • 20
  • 1
    possible duplicate of [How to convert a python utc datetime to a local datetime using only python standard library?](http://stackoverflow.com/questions/4563272/how-to-convert-a-python-utc-datetime-to-a-local-datetime-using-only-python-stand) – Harman Apr 22 '15 at 05:52
  • 1
    take a look at this [SO Question](http://stackoverflow.com/questions/10997577/python-timezone-conversion) – Harman Apr 22 '15 at 05:53

1 Answers1

4
from datetime import datetime, timedelta
n = datetime.now()
print n + timedelta(hours=5)

This code will add 5 hours to the current datetime

amow
  • 2,203
  • 11
  • 19
  • how will it work on this query? abc_date = ticket['abc_date']?(here need to add timedelta or .timedelte will work) abc_date = ticket['abc_date'].split('T')[0] –  Apr 22 '15 at 06:01
  • If the `abc_date` is a datetime field as you said, you can just use `abc_date+=timedelta(hours=5)` – amow Apr 22 '15 at 06:03
  • abc_date = ticket['abc_date'] += timedelta(hours=5) ^ SyntaxError: invalid syntax –  Apr 22 '15 at 06:09
  • after `abc_date = ticket['abc_date'] ` add `abc_date+=timedelta(hours=5)`. or `abc_date = ticket['abc_date'] + timedelta(hours=5) `.Did you even know how to write python? – amow Apr 22 '15 at 06:12
  • i know it and i have tried this add thing before but it gives error abc_date = ticket['abc_date'] + timedelta(hours=5) TypeError: coercing to Unicode: need string or buffer, datetime.timedelta found –  Apr 22 '15 at 06:20
  • @Javi From the error message it is clear, that `ticket['abc_date']` contains a (unicode) string. Your question states that it is a datetime object. If it's a string, you should indicate that in the question. – Audrius Kažukauskas Apr 22 '15 at 06:41