Download MetaTrader 5
To add comments, please log in or register
Deposit to your account via PayPal
barnacle7
376
barnacle7 2015.04.23 12:51 

    

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 

Keith Watford
Moderator
7963
Keith Watford 2015.04.23 22:01  

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

Keith Watford
Moderator
7963
Keith Watford 2015.04.23 22:02  
  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);
honest_knave
969
honest_knave 2015.04.24 00:17  
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! 

whroeder1
13627
whroeder1 2015.04.24 12:55  
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.

/
To add comments, please log in or register