Adding military time to a date

If I have a date, 12/5/21 and I want to at a military time to it, 21:03:36, so that $Date then reads 12/5/21 9:03 PM and not 12/5/21 9:03 AM, how would I do this? I’ve tried internal, “t” and “h” for the formats.

That question doesn’t quite parse. Are you asking about date/times as displayed in Get Info and Displayed Attributes? Or, whether you can alter the time segment of an existing date by using a 24-hr time.

The short answer to the latter, is Yes. Even if your OS locale (as used by Tinderbox as a result) uses AM/PM times, Tinderbox should be able to understand a 24-hr style time. Generally, expressly stating seconds is of little practical use in Tinderbox as they are normally hidden from many displays.

As to the first. Tinderbox is aware of your OS locale settings (number/date formats vary more than imagined). The format used to display data/time is $DisplayedAttributesDateFormat. The choice of setting is most normally done in Document Settings’ Text tab’s Displayed Attributes Date control which offers a pop-up list of 3 possible formats which vary by locale and represent:

  • Locale ‘long’ date with time.
  • Locale ‘long’ date only.
  • Locale ‘short’ date only.

If none of the 3 choices are to your taste, you can set the doc default value of $DisplayedAttributesDateFormat using the Document Inspector’s system tab

To specify a format string in the latter case, use date formatting designators and use that string of characters without any enclosing quotes in the **note: ** box on the Inspector (normally, you do need quotes when using Date.format())

So to get a format like Thu Dec 16, 2021 16:16 the format string you’d place in the box is w M d, y h:mm. Tested here:

Of course, you’ll probably want something different, but that gives you all the parts of the process.

[A note, for any others confused by the term ‘military time’. This appears to be local American English term for the 24-hour clock, i.e. using 06:20 for 6.20 AM and 18:20 for 6.20 PM. As an ex-Signals Officer (albeit RN not USN), I’d assumed it referred to Zulu time (not called UTC) as used for military signals. Hilarity ensued in trying to understand the question.]

I (being English) also immediately thought “military time” meant Zulu. You learn something new every day.

Off topic, but a curiosity for those who are curious: in 18th century Italy they counted the twenty-four hours from the time of the Angelus. They referred to the system of counting the twelve hours from midnight to midday and the twelve from midday to midnight as “German” time.

Sorry, maybe I need to be more clear. I’m not looking to format time.

I have date, e.g. 12/15/2021
I have a time, e.g. 21:03:36.

I want to add the date and time to $Date so that $Date reads 12/14/2021 21:03 PM and NOT 12/15/2021 9:03 AM, as it is currently doing when I try to add the two. Maybe add is the wrong verb, replace might be the better verb. I want to replace the default time of 12:00 with 21:03.

Obviousness is in the eye of the beholder. I’ve been reading a series of books on the history of sorting and indexing. Early alphabetically-sorted indices needed to come with instructions. Why? Because at the time everyone at all literate knew the ‘obvious’ order of things was God / Angelic Beings / Humanity / Animals / Plants / Minerals and people seemed to cope with that just fine (see: The Great Chain of Being). :slight_smile:

@mwra. do you know how I can do this?

Sorry, I missed your last. It’s is still a bit unclear as to whether this is a display format issue or a data manipulation one, but let’s try.

If I understand’ you’ve two texts (i.e. strings)—“12/15/2021” and “21:03:36”—and you want to ‘add’ them such that the date is “12/15/2021 21:03PM” so we need to create the right sort of date and then format it.

I’m just off out, so a quick method:

$MyDate = date("12/15/2021"); // we have a data of 15 Dec with time as at time of running the code
$MyDate.hour = 21; //set the hours time element to 21, i.e. 9PM
$MyDate.minute = 03; //set the minutes time element to 03, i.e. 3 minutes past the hour
$MyDate.second = 36; //set the seconds time element to 36, i.e. 36 seconds past the minute

Time set. To format the date time as a string “12/12/2021 21:03 PM”:

$MyString = $MyDate.format("M0/D/y h:mm:s")

Hope that helps (will be out for some hours now). If not the problem, please give more context as to process, but I think this and previous give you all the parts.

Note: all Date-format attributes has a h/m/s time element even if you never see it. In part this is because the data, under the hood, is simply a number in milliseconds from some universal reference date (I forget which Tinderbox uses but it matters not for this description). The app turns that number in date-times the human user can understand.

No, that does not do it for me. Let me be clearer.

I’m being fed this value: 2021/12/15/21:03:36.

Tinderbox does not recognize this as a valid date. I can parse off the time and get:

Date: 2021/12/15
Time: 21:03:36

Now I want to stitch the two back together and stick it in $Date so that the date reads 12/15/21 21:03 PM.

This will not be a one off occurrence and the input date/time will change.

I can’t figure out how to format the data calculations to get this to work.

ALL Tinderbox dates represent a moment — an instant in time. So their internal representation always has a date and a time.

When strings are converted to dates, Tinderbox attempts to apply dozens of formats until it locates a format that can parse the date. I’ve never beheld the format 2021/12/15/21:03:36, but the related format 2021/12/15 21:03:36 is not unfamiliar. The easiest way to accept this might be to set your system’s local date formatter to adopt this format.

Another trick would be to convert the string to ISO-8601: 2021-12-15 21:03:35.

Another trick would be to extract each field in turn into a var, and use the date constructor $MyDate=date(year,month,day,hour,minute,second)

Perfect, got it. Here is the stamp I worked out.

var vInput="2021/12/15/21:17:45";
var:list vDate=vInput.split("/");
var vYear=vDate.at(o).year;
var vMonth=vDate.at(1);
var vDay=vDate.at(2);
var:list vTime=vDate.at(3).split(":");
var vHour=vTime.at(0);
var vMin=vTime.at(1);
var vSec=vTime.at(2);
$MyDate=date(vYear, vMonth, vDay, vHour, vMin, vSec);

What is producing that date format? It looks like an error so worth reporting to whoever is creating it.

In your solution, is that really an ‘o’ and note a zero:

var vYear=vDate.at(o).year;

You don’t actually need all those vars. An alternative:

var vInput="2021/12/15/21:17:45".replace("(\d{4}/\d{2}/\d{2})/(\d{2}:\d{2}:\d{2})", $1 $2;
// string is now "2021/12/15 21:17:45"
$MyDate = date(vInput);
1 Like

Also, just noticed you’ve a double definition of time there:

Use either 21:03 or 9:03 PM but not both forms mixed. It shouldn’t confuse Tinderbox’s parser but why take the chance. :slight_smile: