How to compare 2 timestamps (date + time + timezone) [duplicate]











up vote
-1
down vote

favorite













This question already has an answer here:




  • Compare two timestamps in Python

    2 answers




I got 2 dates from my database and I need to compare if one date is greater than another one.
I got the date in this format:



2018-11-07 18:00:40.679087+00:00
2018-11-14 00:00:17.908676+00:00


I tried a lot of solutions, but I didn't figured it out how it works fine.



ps.: I got a lot of questions that looks like this one, but in my case I have the timezone to consider that's the difference.










share|improve this question















marked as duplicate by jpp python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 at 12:37


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Take a look here.
    – Vasilis G.
    Nov 19 at 12:02










  • I tried this one, but in my case the format of date contains timezone
    – mr.abdo
    Nov 19 at 12:08










  • Read the docs! %zUTC offset in the form ±HHMM[SS[.ffffff]]. later on: Changed in version 3.7: UTC offsets can have a colon as a separator between hours, minutes and seconds
    – Christian König
    Nov 19 at 12:11












  • That's the point @ChristianKönig. Tku so much.
    – mr.abdo
    Nov 19 at 12:15










  • @Networker in my case I have the timezone to consider, that's the difference. I tried a lot of things that I found here, but none of them works
    – mr.abdo
    Nov 19 at 12:39















up vote
-1
down vote

favorite













This question already has an answer here:




  • Compare two timestamps in Python

    2 answers




I got 2 dates from my database and I need to compare if one date is greater than another one.
I got the date in this format:



2018-11-07 18:00:40.679087+00:00
2018-11-14 00:00:17.908676+00:00


I tried a lot of solutions, but I didn't figured it out how it works fine.



ps.: I got a lot of questions that looks like this one, but in my case I have the timezone to consider that's the difference.










share|improve this question















marked as duplicate by jpp python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 at 12:37


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Take a look here.
    – Vasilis G.
    Nov 19 at 12:02










  • I tried this one, but in my case the format of date contains timezone
    – mr.abdo
    Nov 19 at 12:08










  • Read the docs! %zUTC offset in the form ±HHMM[SS[.ffffff]]. later on: Changed in version 3.7: UTC offsets can have a colon as a separator between hours, minutes and seconds
    – Christian König
    Nov 19 at 12:11












  • That's the point @ChristianKönig. Tku so much.
    – mr.abdo
    Nov 19 at 12:15










  • @Networker in my case I have the timezone to consider, that's the difference. I tried a lot of things that I found here, but none of them works
    – mr.abdo
    Nov 19 at 12:39













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite












This question already has an answer here:




  • Compare two timestamps in Python

    2 answers




I got 2 dates from my database and I need to compare if one date is greater than another one.
I got the date in this format:



2018-11-07 18:00:40.679087+00:00
2018-11-14 00:00:17.908676+00:00


I tried a lot of solutions, but I didn't figured it out how it works fine.



ps.: I got a lot of questions that looks like this one, but in my case I have the timezone to consider that's the difference.










share|improve this question
















This question already has an answer here:




  • Compare two timestamps in Python

    2 answers




I got 2 dates from my database and I need to compare if one date is greater than another one.
I got the date in this format:



2018-11-07 18:00:40.679087+00:00
2018-11-14 00:00:17.908676+00:00


I tried a lot of solutions, but I didn't figured it out how it works fine.



ps.: I got a lot of questions that looks like this one, but in my case I have the timezone to consider that's the difference.





This question already has an answer here:




  • Compare two timestamps in Python

    2 answers








python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 12:17

























asked Nov 19 at 11:59









mr.abdo

74




74




marked as duplicate by jpp python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 at 12:37


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by jpp python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 at 12:37


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Take a look here.
    – Vasilis G.
    Nov 19 at 12:02










  • I tried this one, but in my case the format of date contains timezone
    – mr.abdo
    Nov 19 at 12:08










  • Read the docs! %zUTC offset in the form ±HHMM[SS[.ffffff]]. later on: Changed in version 3.7: UTC offsets can have a colon as a separator between hours, minutes and seconds
    – Christian König
    Nov 19 at 12:11












  • That's the point @ChristianKönig. Tku so much.
    – mr.abdo
    Nov 19 at 12:15










  • @Networker in my case I have the timezone to consider, that's the difference. I tried a lot of things that I found here, but none of them works
    – mr.abdo
    Nov 19 at 12:39


















  • Take a look here.
    – Vasilis G.
    Nov 19 at 12:02










  • I tried this one, but in my case the format of date contains timezone
    – mr.abdo
    Nov 19 at 12:08










  • Read the docs! %zUTC offset in the form ±HHMM[SS[.ffffff]]. later on: Changed in version 3.7: UTC offsets can have a colon as a separator between hours, minutes and seconds
    – Christian König
    Nov 19 at 12:11












  • That's the point @ChristianKönig. Tku so much.
    – mr.abdo
    Nov 19 at 12:15










  • @Networker in my case I have the timezone to consider, that's the difference. I tried a lot of things that I found here, but none of them works
    – mr.abdo
    Nov 19 at 12:39
















Take a look here.
– Vasilis G.
Nov 19 at 12:02




Take a look here.
– Vasilis G.
Nov 19 at 12:02












I tried this one, but in my case the format of date contains timezone
– mr.abdo
Nov 19 at 12:08




I tried this one, but in my case the format of date contains timezone
– mr.abdo
Nov 19 at 12:08












Read the docs! %zUTC offset in the form ±HHMM[SS[.ffffff]]. later on: Changed in version 3.7: UTC offsets can have a colon as a separator between hours, minutes and seconds
– Christian König
Nov 19 at 12:11






Read the docs! %zUTC offset in the form ±HHMM[SS[.ffffff]]. later on: Changed in version 3.7: UTC offsets can have a colon as a separator between hours, minutes and seconds
– Christian König
Nov 19 at 12:11














That's the point @ChristianKönig. Tku so much.
– mr.abdo
Nov 19 at 12:15




That's the point @ChristianKönig. Tku so much.
– mr.abdo
Nov 19 at 12:15












@Networker in my case I have the timezone to consider, that's the difference. I tried a lot of things that I found here, but none of them works
– mr.abdo
Nov 19 at 12:39




@Networker in my case I have the timezone to consider, that's the difference. I tried a lot of things that I found here, but none of them works
– mr.abdo
Nov 19 at 12:39












3 Answers
3






active

oldest

votes

















up vote
2
down vote



accepted










Try this



from dateutil import parser
date1='2018-11-07 18:00:40.679087+00:00'
date2='2018-11-14 00:00:17.908676+00:00'
print parser.parse(date2) - parser.parse(date1)

Result:6 days, 5:59:37.229589





share|improve this answer





















  • That's an elegant way to solve it. Tku
    – mr.abdo
    Nov 19 at 12:27










  • Glad to hear this helps u ;)
    – Narendra Lucky
    Nov 19 at 12:30


















up vote
0
down vote













Try this one (using built-in Python module):



from datetime import datetime

d1 = "2018-11-07 18:00:40.679087+00:00"
d2 = "2018-11-14 00:00:17.908676+00:00"

d1 = d1[:d1.rfind(":")] + d1[d1.rfind(":")+1:]
d2 = d2[:d2.rfind(":")] + d2[d2.rfind(":")+1:]

t1 = datetime.strptime(d1, "%Y-%m-%d %H:%M:%S.%f%z")
t2 = datetime.strptime(d2, "%Y-%m-%d %H:%M:%S.%f%z")

diff = t2 - t1
print(diff)


Output:



6 days, 5:59:37.229589





share|improve this answer



















  • 1




    no need to insert/remove the : for Python 3.7
    – Christian König
    Nov 19 at 12:26












  • I am running Python 3.6 and I am getting ValueError if I don't remove it.
    – Vasilis G.
    Nov 19 at 12:28


















up vote
0
down vote













The : is bugging you, you can parse it like:



d = '2018-11-07 18:00:40.679087+00:00'
d = d[:-3]+d[-2:]
datetime.strptime(d, '%Y-%m-%d %H:%M:%S.%f%z')

datetime.datetime(2018, 11, 7, 18, 0, 40, 679087, tzinfo=datetime.timezone.utc)





share|improve this answer

















  • 1




    no need to insert/remove the : for Python 3.7
    – Christian König
    Nov 19 at 12:27


















3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










Try this



from dateutil import parser
date1='2018-11-07 18:00:40.679087+00:00'
date2='2018-11-14 00:00:17.908676+00:00'
print parser.parse(date2) - parser.parse(date1)

Result:6 days, 5:59:37.229589





share|improve this answer





















  • That's an elegant way to solve it. Tku
    – mr.abdo
    Nov 19 at 12:27










  • Glad to hear this helps u ;)
    – Narendra Lucky
    Nov 19 at 12:30















up vote
2
down vote



accepted










Try this



from dateutil import parser
date1='2018-11-07 18:00:40.679087+00:00'
date2='2018-11-14 00:00:17.908676+00:00'
print parser.parse(date2) - parser.parse(date1)

Result:6 days, 5:59:37.229589





share|improve this answer





















  • That's an elegant way to solve it. Tku
    – mr.abdo
    Nov 19 at 12:27










  • Glad to hear this helps u ;)
    – Narendra Lucky
    Nov 19 at 12:30













up vote
2
down vote



accepted







up vote
2
down vote



accepted






Try this



from dateutil import parser
date1='2018-11-07 18:00:40.679087+00:00'
date2='2018-11-14 00:00:17.908676+00:00'
print parser.parse(date2) - parser.parse(date1)

Result:6 days, 5:59:37.229589





share|improve this answer












Try this



from dateutil import parser
date1='2018-11-07 18:00:40.679087+00:00'
date2='2018-11-14 00:00:17.908676+00:00'
print parser.parse(date2) - parser.parse(date1)

Result:6 days, 5:59:37.229589






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 at 12:16









Narendra Lucky

568




568












  • That's an elegant way to solve it. Tku
    – mr.abdo
    Nov 19 at 12:27










  • Glad to hear this helps u ;)
    – Narendra Lucky
    Nov 19 at 12:30


















  • That's an elegant way to solve it. Tku
    – mr.abdo
    Nov 19 at 12:27










  • Glad to hear this helps u ;)
    – Narendra Lucky
    Nov 19 at 12:30
















That's an elegant way to solve it. Tku
– mr.abdo
Nov 19 at 12:27




That's an elegant way to solve it. Tku
– mr.abdo
Nov 19 at 12:27












Glad to hear this helps u ;)
– Narendra Lucky
Nov 19 at 12:30




Glad to hear this helps u ;)
– Narendra Lucky
Nov 19 at 12:30












up vote
0
down vote













Try this one (using built-in Python module):



from datetime import datetime

d1 = "2018-11-07 18:00:40.679087+00:00"
d2 = "2018-11-14 00:00:17.908676+00:00"

d1 = d1[:d1.rfind(":")] + d1[d1.rfind(":")+1:]
d2 = d2[:d2.rfind(":")] + d2[d2.rfind(":")+1:]

t1 = datetime.strptime(d1, "%Y-%m-%d %H:%M:%S.%f%z")
t2 = datetime.strptime(d2, "%Y-%m-%d %H:%M:%S.%f%z")

diff = t2 - t1
print(diff)


Output:



6 days, 5:59:37.229589





share|improve this answer



















  • 1




    no need to insert/remove the : for Python 3.7
    – Christian König
    Nov 19 at 12:26












  • I am running Python 3.6 and I am getting ValueError if I don't remove it.
    – Vasilis G.
    Nov 19 at 12:28















up vote
0
down vote













Try this one (using built-in Python module):



from datetime import datetime

d1 = "2018-11-07 18:00:40.679087+00:00"
d2 = "2018-11-14 00:00:17.908676+00:00"

d1 = d1[:d1.rfind(":")] + d1[d1.rfind(":")+1:]
d2 = d2[:d2.rfind(":")] + d2[d2.rfind(":")+1:]

t1 = datetime.strptime(d1, "%Y-%m-%d %H:%M:%S.%f%z")
t2 = datetime.strptime(d2, "%Y-%m-%d %H:%M:%S.%f%z")

diff = t2 - t1
print(diff)


Output:



6 days, 5:59:37.229589





share|improve this answer



















  • 1




    no need to insert/remove the : for Python 3.7
    – Christian König
    Nov 19 at 12:26












  • I am running Python 3.6 and I am getting ValueError if I don't remove it.
    – Vasilis G.
    Nov 19 at 12:28













up vote
0
down vote










up vote
0
down vote









Try this one (using built-in Python module):



from datetime import datetime

d1 = "2018-11-07 18:00:40.679087+00:00"
d2 = "2018-11-14 00:00:17.908676+00:00"

d1 = d1[:d1.rfind(":")] + d1[d1.rfind(":")+1:]
d2 = d2[:d2.rfind(":")] + d2[d2.rfind(":")+1:]

t1 = datetime.strptime(d1, "%Y-%m-%d %H:%M:%S.%f%z")
t2 = datetime.strptime(d2, "%Y-%m-%d %H:%M:%S.%f%z")

diff = t2 - t1
print(diff)


Output:



6 days, 5:59:37.229589





share|improve this answer














Try this one (using built-in Python module):



from datetime import datetime

d1 = "2018-11-07 18:00:40.679087+00:00"
d2 = "2018-11-14 00:00:17.908676+00:00"

d1 = d1[:d1.rfind(":")] + d1[d1.rfind(":")+1:]
d2 = d2[:d2.rfind(":")] + d2[d2.rfind(":")+1:]

t1 = datetime.strptime(d1, "%Y-%m-%d %H:%M:%S.%f%z")
t2 = datetime.strptime(d2, "%Y-%m-%d %H:%M:%S.%f%z")

diff = t2 - t1
print(diff)


Output:



6 days, 5:59:37.229589






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 at 12:20

























answered Nov 19 at 12:13









Vasilis G.

2,9402721




2,9402721








  • 1




    no need to insert/remove the : for Python 3.7
    – Christian König
    Nov 19 at 12:26












  • I am running Python 3.6 and I am getting ValueError if I don't remove it.
    – Vasilis G.
    Nov 19 at 12:28














  • 1




    no need to insert/remove the : for Python 3.7
    – Christian König
    Nov 19 at 12:26












  • I am running Python 3.6 and I am getting ValueError if I don't remove it.
    – Vasilis G.
    Nov 19 at 12:28








1




1




no need to insert/remove the : for Python 3.7
– Christian König
Nov 19 at 12:26






no need to insert/remove the : for Python 3.7
– Christian König
Nov 19 at 12:26














I am running Python 3.6 and I am getting ValueError if I don't remove it.
– Vasilis G.
Nov 19 at 12:28




I am running Python 3.6 and I am getting ValueError if I don't remove it.
– Vasilis G.
Nov 19 at 12:28










up vote
0
down vote













The : is bugging you, you can parse it like:



d = '2018-11-07 18:00:40.679087+00:00'
d = d[:-3]+d[-2:]
datetime.strptime(d, '%Y-%m-%d %H:%M:%S.%f%z')

datetime.datetime(2018, 11, 7, 18, 0, 40, 679087, tzinfo=datetime.timezone.utc)





share|improve this answer

















  • 1




    no need to insert/remove the : for Python 3.7
    – Christian König
    Nov 19 at 12:27















up vote
0
down vote













The : is bugging you, you can parse it like:



d = '2018-11-07 18:00:40.679087+00:00'
d = d[:-3]+d[-2:]
datetime.strptime(d, '%Y-%m-%d %H:%M:%S.%f%z')

datetime.datetime(2018, 11, 7, 18, 0, 40, 679087, tzinfo=datetime.timezone.utc)





share|improve this answer

















  • 1




    no need to insert/remove the : for Python 3.7
    – Christian König
    Nov 19 at 12:27













up vote
0
down vote










up vote
0
down vote









The : is bugging you, you can parse it like:



d = '2018-11-07 18:00:40.679087+00:00'
d = d[:-3]+d[-2:]
datetime.strptime(d, '%Y-%m-%d %H:%M:%S.%f%z')

datetime.datetime(2018, 11, 7, 18, 0, 40, 679087, tzinfo=datetime.timezone.utc)





share|improve this answer












The : is bugging you, you can parse it like:



d = '2018-11-07 18:00:40.679087+00:00'
d = d[:-3]+d[-2:]
datetime.strptime(d, '%Y-%m-%d %H:%M:%S.%f%z')

datetime.datetime(2018, 11, 7, 18, 0, 40, 679087, tzinfo=datetime.timezone.utc)






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 at 12:26









Franco Piccolo

1,325611




1,325611








  • 1




    no need to insert/remove the : for Python 3.7
    – Christian König
    Nov 19 at 12:27














  • 1




    no need to insert/remove the : for Python 3.7
    – Christian König
    Nov 19 at 12:27








1




1




no need to insert/remove the : for Python 3.7
– Christian König
Nov 19 at 12:27




no need to insert/remove the : for Python 3.7
– Christian König
Nov 19 at 12:27



Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin