Topic
  • 2 replies
  • Latest Post - ‏2014-02-10T10:03:15Z by davidoff
davidoff
davidoff
51 Posts

Pinned topic prepare bloc to transform dates

‏2014-01-20T12:51:56Z |

Hi

I received as an input a collection of dates and try to transform it on the fly in minutes with a prepare block

   function transformDate(dateIn){
       //2013-12-13 08:10:00 +0100
        dateTime = dateIn.split(" "); 
        date = dateTime[0]; time = dateTime[1];
        dateSplit = date.split("-");
        dateReverse = dateSplit.reverse();
        dateNew = dateReverse.join("/") + " " + time; 
        dateOut = new Date(Date.parse(dateNew));
        return dateOut.getMinutes();
   }
}

SheetConnection sheet("C:/.../dates.xlsx");
starts from SheetRead(sheet,"starts") invoke transformDate;

Since starts are to be converted in minutes, I declared starts as a collection of int in the model.

It seems then impossible to compute, starts since it cannot be at the same time integer and string

Is there a way to capture the transformation directly ?

 

Note that constructing a date through Date.parse is not staightforward : one shall use new Date( Date.parse(...));

 

 

David

Updated on 2014-01-20T13:55:26Z at 2014-01-20T13:55:26Z by davidoff
  • AlexFleischer
    AlexFleischer
    77 Posts
    ACCEPTED ANSWER

    Re: prepare bloc to transform dates

    ‏2014-02-07T12:30:43Z  

    Hi,

    what about

     

    execute
    {
      function transformDate(dateIn){
           
            dateTime = dateIn.split(" ");
            date = dateTime[0]; time = dateTime[1];
            dateSplit = date.split("-");
            dateReverse = dateSplit.reverse();
            dateNew = dateReverse.join("/") + " " + time;
            dateOut = new Date(Date.parse(dateNew));
            return dateOut.getMinutes();
       }
       
       
       function transformDate2(dateIn){
           
            var dateTime2 =  new Date(dateIn);
            return dateTime2.getMinutes();
       }

    writeln(transformDate("13/12/2013 08:10:00"));
    writeln(transformDate2("13/12/2013 08:10:00"));
    }
     

    ?

     

    regards

  • AlexFleischer
    AlexFleischer
    77 Posts

    Re: prepare bloc to transform dates

    ‏2014-02-07T12:30:43Z  

    Hi,

    what about

     

    execute
    {
      function transformDate(dateIn){
           
            dateTime = dateIn.split(" ");
            date = dateTime[0]; time = dateTime[1];
            dateSplit = date.split("-");
            dateReverse = dateSplit.reverse();
            dateNew = dateReverse.join("/") + " " + time;
            dateOut = new Date(Date.parse(dateNew));
            return dateOut.getMinutes();
       }
       
       
       function transformDate2(dateIn){
           
            var dateTime2 =  new Date(dateIn);
            return dateTime2.getMinutes();
       }

    writeln(transformDate("13/12/2013 08:10:00"));
    writeln(transformDate2("13/12/2013 08:10:00"));
    }
     

    ?

     

    regards

  • davidoff
    davidoff
    51 Posts

    Re: prepare bloc to transform dates

    ‏2014-02-10T10:03:15Z  

    Hi,

    what about

     

    execute
    {
      function transformDate(dateIn){
           
            dateTime = dateIn.split(" ");
            date = dateTime[0]; time = dateTime[1];
            dateSplit = date.split("-");
            dateReverse = dateSplit.reverse();
            dateNew = dateReverse.join("/") + " " + time;
            dateOut = new Date(Date.parse(dateNew));
            return dateOut.getMinutes();
       }
       
       
       function transformDate2(dateIn){
           
            var dateTime2 =  new Date(dateIn);
            return dateTime2.getMinutes();
       }

    writeln(transformDate("13/12/2013 08:10:00"));
    writeln(transformDate2("13/12/2013 08:10:00"));
    }
     

    ?

     

    regards

    The conversion works fine.

    However, my initial point was more related to an automatic transform of a date into an int through the prepare block

    I have not been able to do that straightforward. Rather, i declared in the .mod file a tuple with both a string and an int :

     tuple DateInfo{  string date;  int minutes;} {DateInfo} dateInfos = ...;
    

     

    then , in the prepare block :

      function transformDate(truc,name){
           //2013-12-13 08:10:00 +0100 YYYY-MM-DD
            writeln(truc);
            writeln(name);
            writeln("----");
            //for(var rd=1;rd<=7 ;rd++){
            for(var i=0;i< Opl.card(thisOplModel[name]);i++){
                    var record = Opl.item(thisOplModel[name],i);
                    var dateIn = record.date;
                    writeln("dateIn=",dateIn);
                    dateTime = dateIn.split(" "); 
                    date = dateTime[0]; time = dateTime[1];
                    dateSplit = date.split("-");
                    dateNew = dateSplit[1]+"/"+dateSplit[2]+"/"+dateSplit[0] + " " + time;//MM/DD/YYYY
                    dateOut = new Date(Date.parse(dateNew));
                    writeln(dateNew);
                    var minutes = dateOut.getMinutes();
                    writeln("minutes=",minutes);
                    record.minutes = dateOut.getMinutes();
             }
              return true;
       }
      
    }
    
    SheetConnection sheet("uc1.xlsx");
    dateinfos from SheetRead(sheet,"starts") invoke  transformDate;
    

    You see in the code that I transform the date into minutes in order to store it in the attribute minutes of my tuples.I still wonder if there is a more elegant way to load transformed informations directly