$formatdate alias for mIRC

By Vegito on Jun 23, 2015

Syntax:
$formatdate(date, dateformat)

Eexamples:
$formatdate(7/28/2015, mm:dd:yyyy) // returns 07/28/2015
$formatdate(07/08/2015, m:d:yyyy) // returns 7/8/2015
$formatdate(07/8/2015, m:dd:yyyy) // returns 7/08/2015
$formatdate(8/9/2015, dd:mm:yyyy) // returns 08/09/2015
$formatdate(2012/4/8, yyyy:mm:dd) // returns 2012/04/08
$formatdate(2012/4, yyyy:mm) // returns 2012/04

NOTE:
date has to be in the same order as the dateformat.

alias formatdate {
  var %date = $1, %format = $2

  var %i = 1
  while (%i <= 3) {
    tokenize 58 %format
    if ($regex($eval($ $+ %i, 2), ^(dd?|mm?|yyyy)$)) { 
      tokenize 47 %date     
      var %formatDate = $iif($len($regml(1)) isnum 1-2, $base($eval($ $+ %i, 2), 10, 10, $v1), $eval($ $+ %i, 2))
      var %newDate = $instok(%newDate, %formatDate, %i, 32)
    }
    inc %i
  }

  return $gettok(%newDate, 1, 32) $+  $iif($gettok(%newDate, 2, 32), / $+ $v1) $+ $iif($gettok(%newDate, 3, 32), / $+ $v1)
}

Comments

Sign in to comment.
raccoon   -  Jun 28, 2015

Interesting approach, basically adds zero padding to selective tokens.
Just wanted to note that $asctime already supports advanced date formatting...

alias formatdate { return $asctime($ctime($1), $2-) }

The only difference here is the input date must be in the British ordering of d/m/y (eg, dd/mm/yyyy or d/mm/yy etc).
And you have access to more format outputs, as well as time formats. Type /help $asctime
(yy, yyyy, m, mm, mmm, mmmm, d, dd, ddd, dddd, oo) and (hh, H, HH, n, nn, s, ss, t, tt, T, TT, z, zz, zzz)

Examples:
$formatdate(28/7/2015, mm/dd/yyyy) outputs: 07/28/2015
$formatdate(28/7/2015, dddd dd mmmm yyyy) outputs: Tuesday 28 July 2015
$formatdate(28/7/2015 17:23, ddoo of mmmm @ hh:nn tt) outputs: 28th of July @ 05:23 pm

Enjoy :)

PS. If you want a comma in your output format, you will need to use $+ $chr(44) or pass the format in a variable.
var -s %in = 28/7/2015, %fmt = mmmm dd, yyyy, %out = $formatdate(%in,%fmt) for: July 28, 2015

Vegito  -  Jun 28, 2015

Ah thanks, I see I didn't know about $asctime identifier before.

Sign in to comment

Hawkee   -  Jun 23, 2015

It's always good to indicate what this is for in the title, for example "$formatdate alias for mIRC" otherwise nobody will find it on Google.

Vegito  -  Jun 23, 2015

Thanks, updated the title!

Hawkee  -  Jun 23, 2015

Much better!

Sign in to comment

Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.