Questions tagged [date-math]

Questions about performing arithmetic operations on dates and times.

129 questions
141
votes
41 answers

Best way to find the months between two dates

I have the need to be able to accurately find the months between two dates in python. I have a solution that works but its not very good (as in elegant) or fast. dateRange = [datetime.strptime(dateRanges[0], "%Y-%m-%d"),…
Joshkunz
  • 5,575
  • 6
  • 28
  • 24
41
votes
8 answers

How do I do date math in a bash script on OS X Leopard?

I realize I could whip up a little C or Ruby program to do this, but I want my script to have as few dependencies as possible. Given that caveat, how does one do date math in a bash script on OS X? I've seen a post (on another site) where someone…
Gregory Higley
  • 15,923
  • 9
  • 67
  • 96
39
votes
5 answers

How to Subtract Days in MySQL

How can I subtract time in MySQL? For example, today is 16 March; I want to subtract 15 days to reach 1 March. Are there any methods that can be used to subtract 15 days from the current date?
Steven Spielberg
36
votes
2 answers

Comparing dates in rails

Suppose I have a standard Post.first.created_at datetime. Can I compare that directly with a datetime in the format 2009-06-03 16:57:45.608000 -04:00 by doing something like: Post.first.created_at > Time.parse("2009-06-03 16:57:45.608000…
Tom Lehman
  • 85,973
  • 71
  • 200
  • 272
22
votes
2 answers

Return start and end of year given any year

I need two or one (out) C# method that will take any datetime and return the start date of year and end date of year for that year.
abmv
  • 7,042
  • 17
  • 62
  • 100
21
votes
2 answers

Python requests: download only if newer

What is the standard pythonic way to download a new file from a server only if the server copy is newer than the local one? Either my python-search-fu is very weak today, or one really does needs to roll their own date-time parser and comparer like…
matt wilkie
  • 17,268
  • 24
  • 80
  • 115
21
votes
2 answers

php Object of class DateInterval could not be converted to string

i've tried using date_diff and date_create to get a difference from two date that's already converted to string. here's the code: $date_1 = date_create(); $date_now = date_format($date_1, 'Y-m-d'); //echo $date_now .…
Christian Burgos
  • 1,572
  • 9
  • 26
  • 48
14
votes
2 answers

What is the value of the ISO 8601 duration `P1M` (in seconds)?

Suppose I have an ISO 8601 duration, expressed as "P1M". Phrased colloquially, this means "one month." Is there a standard rule for converting this into a number of seconds, assuming the start date is not known? For 30-day months, it might be…
smitelli
  • 6,835
  • 3
  • 31
  • 53
11
votes
4 answers

In Ruby, 12.months != 1.year

Ruby's date/time helpers are useful but I found a discrepancy. It seems that 12.months does not equal 1.year. Check 1.month and you'll find it's equal to 30.days and, of course, 12 * 30.days = 360.days, 5.25 days short of an actual year's length. I…
Warren
  • 535
  • 6
  • 12
10
votes
2 answers

What's the correct None or null entry for a datetime.datetime object in Python?

I'm loading some dates into mongodb using pymongo. Because pymongo does automatic conversions to BSON, I'm working with datetime's datetime.strptime function to turn input strings like "12/04/2013" into Date objects like so: >>>…
Mittenchops
  • 18,633
  • 33
  • 128
  • 246
9
votes
3 answers

PHP Converting DateInterval to int

I'm using this code: $due_date = new DateTime($_POST['due_date']); $today = new DateTime(); $months = $due_date->diff($today); $months->format("%m"); $fine = 0.02 * $price * $months; // i got error in this line $bill = $price + $fine; I want to…
user6247538
8
votes
2 answers

boost local_date_time math wrong?

I'm using Boost's datetime library in my project. I was very happy when I discovered that it has time duration types for hours, days, months, years, etc, and they change their value based on what you're adding them to (i.e. adding 1 month advances…
rmeador
  • 25,504
  • 18
  • 62
  • 103
8
votes
2 answers

Php, date manipulation?

I am new to php. And I would like to know some of the date manipulation things in php. // Get Current date $date = date("Y-m-d"); What if I want to subtract current date and a specific date, lets say "today - 2008-06-26"? How to do date math…
jingleboy99
  • 705
  • 3
  • 9
  • 16
7
votes
4 answers

easy way to add 1 month to a time_t in C/C++

I have some code that uses the Oracle function add_months to increment a Date by X number of months. I now need to re-implement the same logic in a C / C++ function. For reasons I don't want/need to go into I can't simply issue a query to oracle to…
Glen
  • 21,816
  • 3
  • 61
  • 76
6
votes
1 answer

how to add 48 hours to a timestamp

I am using this code to minus 48 hours from a timestamp echo date($result2["datetime"], strtotime("-48 hours")); This works fine, i want to add 48 hours, i have tried: echo date($result2["datetime"], strtotime("+48 hours")); I have echoed…
user3843997
  • 205
  • 1
  • 5
  • 9
1
2 3
8 9