Log in

No account? Create an account

Previous Entry | Next Entry

Changing the Date and Time Format, Simplified

  • Mar. 12th, 2007 at 12:58 PM
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;
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.


( 8 comments — Leave a comment )
Jul. 13th, 2007 11:43 am (UTC)
Thanks, this really helps! One question, though: I see that there's the option to have just the date show, not the time. How do I implement this through my theme layer? (I'm not using the Customization Wizard.) I looked through the layer source but couldn't find a likely-looking property to set.
Jul. 13th, 2007 12:48 pm (UTC)
Look at the functions that call this one, which are print_entry() and print_comment(). This function just prints whatever is asked for based on the booleans in the arguments, so change the fourth one from "true" to "false" wherever you want to disable printing the time. It's probably something that should be controlled with properties, but that wasn't up to me.
Jul. 13th, 2007 03:20 pm (UTC)
Yeah, that'd work. Thanks a million! Alternately, I could just change the lang_posted_by_date_and_time function so that it simply doesn't print the time.
Jul. 13th, 2007 04:12 pm (UTC)
I suppose so, if you didn't care about having the time printed in comments, too. =P
Oct. 13th, 2007 11:34 am (UTC)
I'm very interested by this customization, but what do I have to do to have a time marked as, for instance, 22:50 instead of 10:50PM. I don't know what to write in the blue lines...

Thanks for your help
Oct. 13th, 2007 01:55 pm (UTC)
There's a link to the S2 docs at the end of the post that lists all of the different variables that are available. There's also one here in s2howto.
Oct. 13th, 2007 02:28 pm (UTC)
thank you, I did read the end of your post, but I didn't understand it, however, the link you gave me was so much clearer for someone like me who is computer illeterate!
Oct. 13th, 2007 03:22 pm (UTC)
No problem. I edited the original instructions as well.
( 8 comments — Leave a comment )

Latest Month

March 2016

S2 Help Communities


Powered by LiveJournal.com
Designed by chasethestars