Page 1 of 1

Convert Metadata field (Date) to Weekday

Posted: Fri Oct 09, 2015 2:41 pm
by lombert
It's a similar problem as the other topic "Filter on Weekday" (http://forum.enfocus.com/viewtopic.php?f=13&t=1162). But I have a date in one xml-file in the format "yyyyMMddHHmm" (201510091437).

I want to convert that to is corresponding weekday. Eg. Friday as the example.

Can I get it to work with "Set hierarch path" it would be great.

Is it possible?

Re: Convert Metadata field (Date) to Weekday

Posted: Fri Oct 09, 2015 3:51 pm
by gabrielp
This should do it:

Code: Select all

	var getDay = function( dateString, debug )
	{
		debug = typeof debug !== 'undefined' ? debug : false;
	
	   // Pattern for parsing the string
	   var regex = /(\d{4})+(\d{2})+(\d{2})+(\d{2})+(\d{2})/;
	   regex.search(dateString);
	   var parsedYear = regex.cap(1);
	   var parsedMonth = regex.cap(2);
	   var parsedDay = regex.cap(3);
	   var parsedHour = regex.cap(4);
	   var parsedMinute = regex.cap(5);
	   
		// Log   
	   if(debug == true) s.log(2, parsedYear + ', ' + parsedMonth + ', ' + parsedDay + ', ' + parsedHour + ', ' + parsedMinute);
	   
	   // Convert to date
	   var date = new Date( parsedYear, parsedMonth, parsedDay, parsedHour, parsedMinute );
	   
	   // Get the day of the week
	   var day = date.getDay();
	   
	   // Convert day code to full day
	   var weekDayValues = {
	       1: 'Sunday',
	       2: 'Monday',
	       3: 'Tuesday',
	       4: 'Wednesday',
	       5: 'Thursday',
	       6: 'Friday',
	       7: 'Saturday'
	   };
	   
	   // Return
	   return weekDayValues[day];
	   
	};
	
	// Log
   s.log(2, '201510091437 becomes: ' + getDay( '201510091437' ) );
   s.log(2, '201510121437 becomes: ' + getDay( '201510121437' ) );

Re: Convert Metadata field (Date) to Weekday

Posted: Mon Oct 12, 2015 9:17 am
by lombert
Thanks!

But I got Sunday what ever date I put in the first line..

"201510091437" gets Sunday
"201510121437" gets Sunday

And it's Monday here now. :)

Re: Convert Metadata field (Date) to Weekday

Posted: Mon Oct 12, 2015 5:00 pm
by gabrielp
lombert wrote:Thanks!

But I got Sunday what ever date I put in the first line..

"201510091437" gets Sunday
"201510121437" gets Sunday

And it's Monday here now. :)
I should have started with regex.cap(1) instead of regex.cap(0). I've updated the script in my original post. Let me know if it that works.

Edit: put it inside a function so you can just call getDay( '201510091437' )

Re: Convert Metadata field (Date) to Weekday

Posted: Tue Oct 13, 2015 12:50 pm
by lombert
The new one gives me this;

201510121437 becomes: Sunday -> Should be Monday
201510091437 becomes: Thursday -> Should be Friday

But this may be a system thing?

In the script the week starts with Sunday, is that the normal way? In Sweden we have Monday as first day? I mean that in your american system first day is sunday and everything works, but I need to change to Monday -> Sunday?

Re: Convert Metadata field (Date) to Weekday

Posted: Tue Oct 13, 2015 1:31 pm
by lombert
I'm a realy noob then it came to script.. :(

If I change the settings to start with monday it is working correct.
But how can I now use this in my flow?

The metadata (XML) is "[Metadata.Text:Path="/order/deadline",Dataset="Xml",Model="XML"]" and I want it to be set as an variabel in "Set hierarch path" so I can set a folder with the correct weekday then the files arrives.

Re: Convert Metadata field (Date) to Weekday

Posted: Tue Oct 13, 2015 3:08 pm
by gabrielp
lombert wrote:But how can I now use this in my flow?

The metadata (XML) is "[Metadata.Text:Path="/order/deadline",Dataset="Xml",Model="XML"]" and I want it to be set as an variabel in "Set hierarch path" so I can set a folder with the correct weekday then the files arrives.
Ok... clone this repo (or just download the .sscript file): https://github.com/dominickp/SwitchParseDayOfWeek

Drop in a new scripting configurator and browse for that script. Then, for the "Date String" attribute, choose "Define single-line text with variables". Metadata > Text > "Path >" > Build location path, and select your element that is the timestamp. This should probably be [Metadata.Text:Path="/order/deadline",Dataset="Xml",Model="XML"].

For the "Private Data Key" property, set that to anything you want. Let's call it "LombertsPDKey".

Just pass your job through that script and then in your archive hierarchy, you can now use [Job.PrivateData:Key="LombertsPDKey"] (found in Job > PrivateData within 'Define single-line text with variables') which will be the day that was parsed by the script: Thursday, for example. So just drop that in somewhere where defining your hierarchy path.

Re: Convert Metadata field (Date) to Weekday

Posted: Wed Oct 14, 2015 9:46 am
by lombert
Big Thanks!
Could I send you a cake I would! :)

I think I can get this to work now.

Re: Convert Metadata field (Date) to Weekday

Posted: Fri Feb 19, 2016 4:05 pm
by lombert
Have a new question about the same metadata field.

I have tried to check if the date is "yesterday/older" or "tomorrow/younger" in a "Defining a condition with variables", but if I set the metadata to be a "date" it will not respond.
In the manual it says the string has to be "yyyy-MM-dd.. ..", but I can't change the metadata, it came "yyyyMMdd.. .."..

An ISO 8601 formatted date/time (equivalent to format string "yyyy-MM-ddThh:mm:ss.zzz") or any portion thereof
Enter a format string in the Format argument of the variable to specify which portion of the date and/or time should take part in the comparison
For example, you may enter "yyyy-MM" for the format string and "2007-08" as the comparison value to compare year and month


Why can't Switch recognise that as an date?