Mister Baltimore (av8rmike) wrote in s2expressive,
Mister Baltimore
av8rmike
s2expressive

Changing the Date and Time Format, Simplified

Not to step on nimoloth's similar post, but I've always thought that the date and time printing function could be simplified a bit. So, here is my take on it, which I'm sure could be streamlined a little more.
To use, create a custom theme layer if you don't already have one, then copy in this code and change the blue variables to suit your customization needs.
function lang_posted_by_date_and_time(EntryLite e, bool showposter, bool showdate, bool showtime) : string {
    var string posted = "";
    var string datefmt_ent = $*lang_fmt_date_med;        # Date format to use for entries.
                             # Default is $*lang_fmt_date_med (%%mon%%. %%dayord%%, %%yyyy%%)
    var string timefmt_ent = "%%h%%:%%min%% %%A%%M";        # Time format to use for entries.
                             # Default is %%h%%:%%min%% %%A%%M
    var string datefmt_com = "med";        # Date format to use for comments.
                             # Default is $*lang_fmt_date_med (%%mon%%. %%dayord%%, %%yyyy%%)
    var string timefmt_com = "short";        # Date format to use for comments.
                             # Default is "short", which looks like entry time default with lowercase "am/pm".

    if ($showdate and $showtime) { # Show both on entries and comments, with "at" in entries
        if ($e.depth > 0) { # If $e is a comment
            $posted = $e->time_display($datefmt_com, $timefmt_com);
        } else {
            var string date_time = $datefmt_ent + " at " + $timefmt_ent;
            $posted = $e->time_display($date_time, "none");
        }
    } else {
        if ($showdate) { # Only showing date
            $posted = $e->time_display($datefmt_ent, "none");
        } else { # Only showing time (date is already shown in subject)
            $posted = $e->time_display("none", $timefmt_ent);
        }
    }
    return $posted;
}

I've split the time and date printing for entries and comments into four separate strings, individually customizable. The time/date options from the S2 Core are here and here in s2howto, but that doesn't really explain how to customize this function. For example, the layout default date format is "med" in that table, stored in the layout property $*lay_fmt_date_med. To change the date format on entries to use the spelled-out month, change this line:
var string datefmt_ent = $*lay_fmt_date_med;
to
var string datefmt_ent = "long";
with the quotes. You can use any of the strings in the "Named Format" column for time or date respectively, but notice that there is only one named time format anyway.
To build a completely custom time or date format, just string the variables from the bottom table, using the "named formats" table as a guide. For example, something like:
var string timefmt_ent = "%%HH%%%%min%%";
would format the time in entries like "1836", with 24-hour time and no colon separator.
Tags: !tutorial, $acct level:paid or perm, entries:timestamp, how to:instructions, s2:theme layer
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

  • 8 comments