Author Topic: Getting crazy with dates (regex)  (Read 6370 times)

theta_wave

  • Sr. Member
  • ****
  • Posts: 680
So, I wanted to try to get fancy with my dates, namely wanting to make it display as either "MMMM D, YYYY", "MMMM YYYY" or "YYYY".  In my setup,  <Year> is stored as 4-digit value and the month and year are stored in another tag (<DateCustom>) as "month.day".  I used <DateCustom> for creating folder names and sorting under Mediamonkey.  Using the virtualtag <DateTest> below, I get the following result: YYYY, 6-digit YYYYMM, or 8-digit YYYYMMDD.  The reason why there's a 6-digit and 8-digit result is due to $Date returning a null result when faced with "00" (Y2K holdover?).  So, here's the virtualtags:

Code
DateTest: $IsNull(<DateCustom>,<Year (yyyy)>,$If(<DateCustom>=00.00,<Year (yyyy)>,$If($Right(<DateCustom>,2)=00,<Year (yyyy)>$Left(<DateCustom>,2),<Year (yyyy)>$Replace(<DateCustom>,.,))))

For $Date, I used the following:
* 8-digit YYYYMMDD: $Date($Left(<DateTest>,4)/$Left($Right(<DateTest>,4),2)/$Right(<DateTest>,2),MMMM d"," yyyy)
* 6-digit YYYYMM: $Date($Left(<DateTest>,4)/$Right(<DateTest>,2),MMMM yyyy)

Everything works so far, but the difficulty is finding a way to have the parser recognize the 6-digit and 8-digit strings and apply the appropriate function.  Ideally, I would use regex (^[\d]{6}$ or ^[\d]{8}$) to sniff out those values (basically a search for 6- or 8-length digits), but it would only return T or F as values.  Well, here comes $If(value,false,true) function to the rescue!

Here is how I manage to get the date to show up as either "YYYY", "MMMM YYYY" or "MMMM d, YYYY" as seen in the attached pic below:

Code
DateFormat: $IsNull(<DateCustom>,<Year (yyyy)>,$If($IsMatch(<DateTest>,"^[\d]{6}$")=T, $Date($Left(<DateTest>,4)/$Right(<DateTest>,2),MMMM yyyy), $Date($Left(<DateTest>,4)/$Left($Right(<DateTest>,4),2)/$Right(<DateTest>,2),MMMM d"," yyyy)))

I hope someone here finds this useful.



Here's an extra bit of code if someone simply wants M/d/YYYY (derived from <DateCustom> = month.day, including "month.00" [i.e. 07.00])
Code
ReleaseDate: $IsNull(<DateCustom>,<Year (yyyy)>,$If(<DateCustom>=00.00,<Year (yyyy)>,$If($Right(<DateCustom>,2)=00,$If($Left(<DateCustom>,1)=0,$Right($Left(<DateCustom>,2),1)/<Year (yyyy)>,$Left(<DateCustom>,2)/<Year (yyyy)>),$Replace($Replace(<DateCustom>,0,),.,/)/<Year (yyyy)>)))
Last Edit: January 20, 2016, 05:32:29 AM by ssri

Bee-liever

  • Member
  • Sr. Member
  • *****
  • Posts: 3833
  • MB Version: 3.6.8849 P
I hope someone here finds this useful.
I certainly did!

With insight from this I now get what Steven meant with:
why not just use $Date(<field>,"dd MMMM yyyy")
I was able to change my over-the-top function (see here) to a much easier

Code
Released: $If($Right(<Release Date Sort>,4)=0000,<YYYY>,$If($Right($Right(<Release Date Sort>,4),2)=00,$Date($Left($Right(<Release Date Sort>,4),2)/$Left(<Release Date Sort>,4),MMM yyyy),$Date($Right(<Release Date Sort>,2)/$Left($Right(<Release Date Sort>,4),2)/$Left(<Release Date Sort>,4),d MMM yyyy)))

Thanks heaps :)
MusicBee and my library - Making bee-utiful music together

theta_wave

  • Sr. Member
  • ****
  • Posts: 680
I hope someone here finds this useful.
I certainly did!
Thanks heaps :)
No problem, your examples certainly showed me the way.  To be honest, this started out as a help thread because I was stuck until I noticed the availability of a function incorporating regex.  Also, I started really using MB last weekend, rather than tinkering with it briefly like in the past, and I knew next to nothing how functions work here, but from your thread and the wiki I started to think about what Steven was saying and your mentioning of $Date's inability to handle double zeroes.  It is really awesome how flexible MusicBee is when it comes to customizing how one wants their collection presented. 

Your code looks much nicer than the behemoth that you provided to me in your thread ;)  Good call on using MMM rather than MMMM.

Oh yeah, I had to make a slight correction below (bolded).  I noticed how a few dates were not showing up on my end.  That may or may not be applicable to everyone else but in the end I thought adding it might be helpful.  Cheers!

DateFormat: $IsNull(<DateCustom>,<Year (yyyy)>,$If(<DateCustom>="00.00",<Year>,$If($IsMatch(<DateTest>,"^[\d]{6}$")="T",$Date($Left(<DateTest>,4)/$Right(<DateTest>,2),MMMM yyyy),$Date($Left(<DateTest>,4)/$Left($Right(<DateTest>,4),2)/$Right(<DateTest>,2),MMMM d"," yyyy))))
Last Edit: January 20, 2016, 08:45:10 AM by ssri

theta_wave

  • Sr. Member
  • ****
  • Posts: 680
God I'm dumb, here's a much better code for those having <Year> and <Month.Day> fields (the latter being <DateCustom> below):

Code
$IsNull(<DateCustom>,<Year>,$If(<DateCustom>="00.00",<Year>,$If($Right(<DateCustom>,2)="00",$Date(<Year>/$Split(<DateCustom>,.,1),MMM yyyy),$Date(<Year>/$Left(<DateCustom>,2)/$Right(<DateCustom>,2),MMM d"," yyyy))))