mIRC Homepage
Posted By: billythekid Calculating dates. - 06/12/07 12:01 PM
this is a pain in the ascii. I've had a quick go on $datediff and $datexpander but neither output what I'm trying to do, which is...

work out what date was exactly 6 months ago and return it.

I thought a simple calc would work, normally it would, i.e 6 months ago from today returns 6th June 2007, but there's nothing simple about months! i.e 6 months ago from aug 30th would return feb 30th, I'd want that to return feb 28th. it's not 1st or 2nd of march because that's less than 6 months. If anyone has any ideas I'd love to hear them. Should I go with the calc and add in checks for each possible anomaly? also there's jan - may which will all return negative dates! It could be too early for me today of course and the answer is glaring at me...

I'm almost ready to hit the sockets for a date calculator website...but I thought I'd throw it out here first

thanks

btk
Posted By: qwerty Re: Calculating dates. - 06/12/07 12:47 PM
The trickiest part in such cases is not the actual coding but the exact definition of the problem itself. For example, you said that 6 months before aug 30 should be feb 28. But what about 6 months before aug 28? Would that also be feb 28, ie is it acceptable for two different dates to have the same 6-months-ago date?
Posted By: billythekid Re: Calculating dates. - 06/12/07 12:56 PM
yeah that is the case. if it were to be worked out on days/weeks no problem but because it's for months which all have varying days it's troublesome. I'm thinking (since it's for a bot) to have it return "6 months ago was DATE (if that's not possible the last date of MONTH)" just for sanity!

btk
Posted By: Mpdreamz Re: Calculating dates. - 06/12/07 01:38 PM
Code:
/*
$dateadd([DATE,]AMOUNT,FACTOR)
*/
alias dateadd {
  var -s %t,%r,%n $crlf,%com = dd $+ $ticks,%d = $iif($3,$qt($1),Date),%e = $iif($3,$2,$1),%f = $qt($iif($3,$3,$2))
  if ($istok(95 98,$os,32)) return 2
  %t = Dim sd %n Dim ret %n sd = %d %n 
  %t = %t ret = DateAdd( $qt($replace($upper(%f),y,YYYY,w,WW)) , %e , sd) %n MsgBox(ret)
  echo -a %t
  comopen %com MSScriptControl.ScriptControl
  if ($comerr) return E_COM
  noop $com(%com,language,4,bstr*,vbscript)
  if ($com(%com,executestatement,1,bstr*,%t)) && ($com(%com,eval,1,bstr*,ret)) %r = $com(%com).result 
  else return 1 $v1 $v2
  :error
  if ($com(%com)) .comclose %com $com(%com,reset,1)
  return %r
}


//echo -a $dateadd(8/31/2007,-6,m)


Code is unfinished it justs msgbox's the date but the result is screwwy i need to get off now and have a look at it later or someone else might fix it in the mean time smile
Posted By: billythekid Re: Calculating dates. - 06/12/07 01:46 PM
mmmm that's great just need it passed into mirc somehow.

thanks for looking into this Mpdreamz cool
Posted By: genius_at_work Re: Calculating dates. - 06/12/07 03:35 PM
Try this code:

Code:
alias monthsago {
  ; $1 = months
  tokenize 32 $abs($gettok($1,1,32))

  var %cm = $asctime(m)
  var %cd = $asctime(d)
  var %cy = $asctime(yyyy)

  var %nm = $calc(%cm - ($1 % 12))

  var %dy = $calc($1 / 12)
  echo -a > $calc((%dy - $int(%dy)) * 12)
  if ($calc((%dy - $int(%dy)) * 12) > %cm) inc %dy
  echo -a dy 1: %dy
  var %ny = $calc(%cy - $int(%dy))

  var %nd = %cd
  while ((%nd > 28) && ($ctime($+(%nd,/,%nm,/,%ny)) == $null)) {
    dec %nd
    echo -a %nd
  }

  return %nd %nm %ny 
}



Syntax:
$monthsago(<months>)

Returns:
<day> <month> <year>
(numerical form)

Adjusts for days that don't exist in a specific month (ex. Feb 30). Accounts for leap years. Converts negative input to positive.

*Not fully tested. Report any bugs.

-genius_at_work
Posted By: billythekid Re: Calculating dates. - 06/12/07 05:01 PM
wow, you're nick isn't a mis-nomer!

Fantastic does exactly what it should! (and I couldn't!)

I'm sticking it in, if I find any bugs I'll post them but it looks like it rocks!

btk
Posted By: billythekid Re: Calculating dates. - 07/01/08 05:29 PM
today it returned august 7th, haven't had the chance to check it out yet...

btk
Posted By: genius_at_work Re: Calculating dates. - 08/01/08 04:18 AM
And?

-genius_at_work
© mIRC Discussion Forums