How to Extract from a String

 

    

I have date in CSV  file with the Date formatted as:  month /day /year .

How can I extract the  month / day / year  ( 04/ 23/ 2015 )  from the String ?

I can't use anything that converts it to Datetime otherwise it becomes day/month/year format.

 

Thanks 

 

I'm not really sure what you want

 EDIT: I don't know what goes on - Sometimes I include SRC and when I post, it has disappeared

 
  string source="4/23/2015";
  string temp_array[];
  ushort sep=StringGetCharacter("/",0);
  StringSplit(source,sep,temp_array);
  int day=StrToInteger(temp_array[1]);
  int month=StrToInteger(temp_array[0]);
  int year=StrToInteger(temp_array[2]);
  Print("Month=",month,", Day=",day,", Year=",year);
  datetime date=StringToTime(temp_array[2]+"."+temp_array[0]+"."+temp_array[1]);
  Print(date);
 
GumRai:

I'm not really sure what you want

 EDIT: I don't know what goes on - Sometimes I include SRC and when I post, it has disappeared

It happens with links and images to.

It seems to happen if inserting code/link/image is the last thing you do before submitting.

Consequently I now hit enter a few times before submit! 

 
barnacle7:

I have date in CSV file with the Date formatted as: month /day /year .

How can I extract the  month / day / year  ( 04/ 23/ 2015 )  from the String ?

I can't use anything that converts it to Datetime otherwise it becomes day/month/year format.

  1. You can't use the built in StrToTime - MQL4 Documentation to create a datetime because it expects ""yyyy.mm.dd hh:mi" So you reformat your string
  2. If the string has spaces (as you posted) trim them after splitting

Not compiled, not tested.

string MDY_To_YMD(const string& mdy, ushort sep='/'){     // "month/day/year"
  string split[]; if(3 != StringSplit(mdy, sep, split) ) return 0;

  int year = StrToInteger(split[2]);
  if(year < 100) year += year < 50 ? 2000 : 1900;           // 99->1999 01->2001

  return StringFormat("%i.%s.%s" year, split[0], split[1]); // "Year.Month.day"
}
datetime MdyToTime(const string& mdy, ushort sep='/'){
  return StringToTime( MDY_To_YMD(ymd, sep) );
}
string   ymd  = "4/25/15";
datetime when = MdyToTime(ymd);

Not compiled, not tested.

Reason: