need help formating date-time string

To add comments, please log in or register
marth tanaka
232
marth tanaka  
currently, using this
date1 = StringSubstr(date1,0,4) + "-" + StringSubstr(date1,5,2) + "-" + StringSubstr(date1,8,2);


       string time1 = TimeToStr(iTime(ccy,tf,i),TIME_MINUTES);
       FileWrite(handle, date1 + " "+ time1);
which gives me, for example, this
2018-07-20 21:00
However, I am trying to format it like this instead,

2019-04-26 02-AM
So far Im thinking of using something like this,

suffix = " am";
if (hour > 12) {
hour -= 12;
suffix = " pm";
}
hourstring = hour + suffix;
Any ideas guys?
Seng Joo Thio
595
Seng Joo Thio  
marth tanaka:
currently, using this
which gives me, for example, this
However, I am trying to format it like this instead,

So far Im thinking of using something like this,

Any ideas guys?

You need to take care of special cases: 0 and 12 hours, too. Can try this:

   int hourInt = Hour();
   string hourStr = (hourInt%12==0)? "12": IntegerToString((hourInt>12)?hourInt-12:hourInt);
   hourStr = (hourInt>=12)? (hourStr+" pm"): (hourStr+" am");
   Print (hourStr);   
marth tanaka
232
marth tanaka  
Seng Joo Thio:

You need to take care of special cases: 0 and 12 hours, too. Can try this:

worked perfectly, thank you!
To add comments, please log in or register