%time2=getDateTime();%time=substr(%time2,0,1);if(%time<=12){ %a=am; %time=%time@subStr(%time2,2,8)@%a;}else if(%time>=13){ %a=pm; %time=%time-12@subStr(%time2,2,8)@%a;}
Code: [Select]%time2=getDateTime();%time=substr(%time2,0,1);if(%time<=12){ %a=am; %time=%time@subStr(%time2,2,8)@%a;}else if(%time>=13){ %a=pm; %time=%time-12@subStr(%time2,2,8)@%a;}Something like that maybe?
function getTime(){ %hour = getSubStr(getWord(getDateTime(), 1), 0, strPos(getWord(getDateTime(), 1), ":")); %minute = getSubStr(getWord(getDateTime(), 1), 3, 2); %word = "AM"; if(%hour >= 12 && %hour != 24) { if(%hour >= 13) { %hour -= 12; } %word = "PM"; } return %hour @ ":" @ %minute SPC %word;}
Here's one I wrote around 2 days ago (gives you HOUR MINUTE AM/PM):function get12Time(){ nextToken(nextToken(getWord(getDateTime(), 1), "hour", ":"), "minutes", ":"); %new = %hour; return ((%hour > 12 ? %hour-=12 : %hour) SPC %minutes SPC (%new > 12 ? "PM" : "AM"));}If anyone would kindly help me into getting this to fit into one line, I would appreciate that.
Close, but not quite. subStr (it's getSubStr()), isn't a default function, and once you checked if %time is less than 12, you can just use else instead of an else if. Also, you didn't have whatever this code is return anything, and all it does is define values, and never does anything with them.
virtual string getSubStr(string str, int start, int numChars) {}
Yeah sorry, I was close. I don't write things in to be copied and pasted right in, I tend to like to give general ideas. Also, getSubStr is default.
1. That won't even work. %hour is an undefined variable.2. The whole nextToken thing you're doing does not even return the right thing.3. You're not even setting the nextToken thing to a variable.4. Why would you want it all in one line? Not very readable that way.
The first 3 of these are false. Test the actual code. The 2nd arg is what sets the variable. Go ahead, try it. ==>%this = "12:14:05"; nextToken(%this, "asdf", ":"); echo(%asdf);14:05I have tested my code many times, the entire thing works just fine. Actually run it and see what it does before you start saying "it won't work", because it does.
Weird. There are almost no default functions in TorqueScript that set a variable. Almost all of them just return the new value, not set it. Also, when I tested it, I just echoed the function. Because I expected it to return the result, not set it to a variable.
function get12Time(){ nextToken(nextToken(getWord(getDateTime(), 1), "hour", ":"), "minutes", ":"); %new = %hour; return ((%hour > 12 ? %hour-=12 : %hour) SPC %minutes SPC (%new > 12 ? "PM" : "AM"));}If anyone would kindly help me into getting this to fit into one line, I would appreciate that.
function get12Time(){nextToken(nextToken(getWord(getDateTime(), 1), "hour", ":"), "minutes", ":");%new = %hour;return ((%hour > 12 ? %hour-=12 : %hour) SPC %minutes SPC (%new > 12 ? "PM" : "AM"));}
If anyone would kindly help me into getting this to fit into one line, I would appreciate that.