1

As you can see the question above, I was wondering if IDL is able to add or subtract days / months / years to a given date.

For example:

given_date = anytim('01-jan-2000')
print, given_date

1-Jan-2000 00:00:00.000

When I would add 2 weeks to the given_date, then this date should appear:

15-Jan-2000 00:00:00.000

I was already looking for a solution for this problem, but I unfortunately couldn't find any solution.

Note: I am using a normal calendar date, not the julian date.

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
TheHeroOfTime
  • 751
  • 4
  • 9
  • 23

2 Answers2

2

Are you only concerned with dates after 1582? Is accuracy to the second important?

The ANYTIM routine is not part of the IDL distribution. Possibly there are third party routines to handle time increments, but I don't know of any builtin to the IDL library.

By default, which you are using, ANYTIM returns seconds from Jan 1, 1979. So to add/subtract some number of days, weeks, or years, you could calculate the number of seconds in the time interval. Of course, this does not take into account leap seconds/years (but leap years are fairly easy to take into account, leap seconds requires a database of when they were added). And adding months is going to require determining which month so to determine the number of days in it.

mgalloy
  • 2,356
  • 1
  • 12
  • 10
1

IDL can convert to and from Julian dates using JULDAY and CALDAT.

You may also read and write Julian dates (which are doubles or long integers) to and from strings using the format keyword to PRINT, STRING, and READS.

You'll want to use the (C()) calendar date format code.

format='(c(cdi0,"-",cMoa,"-"cyi04," ",cHi02,":",cmi02,":",csf06.3))'

date = julday(1, 1, 2000)
print, date, format=format
; 1-Jan-2000 00:00:00.000

date = date + 14
print, date, format=format
; 15-Jan-2000 00:00:00.000
Pi Marillion
  • 4,465
  • 1
  • 19
  • 20
  • Thanks for your answer. Your solution looks quite good, but there is one strange thing that happens, when the date is printing out. The result for me is not `1-Jan-2000 00:00:00.000`, but it prints this: `1-Jan-2000 12:00: 0.000`. So I tried to set also the time to 0 and this prints out: `1-Jan-2000 00:00: 0.000`. Does this have something to do with the format or something with **JULDAY**? – TheHeroOfTime Feb 10 '17 at 12:59
  • 1
    Whoops! I used `csf6.3`, when I should have used `csf06.3`. – Pi Marillion Feb 10 '17 at 23:13
  • 1
    Yes, from the `JULDAY` documentation: "The JULDAY function calculates the Julian Date (which begins at noon) for the specified date." – mgalloy Feb 12 '17 at 23:06