Date and time language

Hi,
I’m using this Insert Time and Date Macro:

Is there a way to change the date and time format without changing the Region in macOS preferences?
I use English as the primary Language & Region, but want to be able to switch to Swedish occasionally, to show days and months in that language using date and time macros.

I’ve seen some solutions using Terminal, but that involves a restart, and I want to be able to switch back and forth between languages quickly.

Hello Hakan (@glante) :wave:

If I understand you correctly you just want to perform some date math - right ?!

If that’s the case I can use the Offset time in a calculation with variables and manipulate the output string of KM‘s date and time Tokens to get the desired result.

I have no chance getting to my Mac because I am not at home but I hope this may help you

Greetings from Germany

Tobias

1 Like

Thanks Tobias!
I just want to be able to choose language, English or Swedish, for the output.
For example:
Tuesday, 20 February 2024
vs
Tisdag, 20 Februari 2024

With an example like yours, where there are so few search terms, it can at the very least be brute forced by translation, setting it up as a Search and replace within a For each, like this:

Insert Time and Date — Translating English names- of weekday and month- to Swedish.kmmacros (4.3 KB)
(v11.0.2)

Macro Image

Disabling the green Translation group inputs the time and date untranslated.

3 Likes

I use a combination of Apple Shortcuts and Keyboard Maestro to do this:

4 Likes

and another approach is to experiment with passing different two-character locale strings like {"sw" "fr" "zh" "he" "en"}
drawn from (List of ISO 639 language codes - Wikipedia)

to Apple Foundation Class methods through a Keyboard Maestro Execute JavaScript for Automation action:

Time string for 2 char locale code.kmmacros (3.8 KB)


Expand disclosure triangle to view JS source
    // const kmvar = {"local_twoCharLocaleName": "sw"};

    const nsDateFormatter = $.NSDateFormatter.alloc.init;

    nsDateFormatter.setDateStyle(
        $.NSDateFormatterLongStyle
    );

    nsDateFormatter.setTimeStyle(
        $.NSDateFormatterShortStyle
    );

    nsDateFormatter.setLocale(
        $.NSLocale.alloc.initWithLocaleIdentifier(
            kmvar.local_twoCharLocaleName
        )
    );

    return ObjC.unwrap(
        nsDateFormatter.stringFromDate(
            $.NSDate.alloc.init
        )
    );
2 Likes

Or, using any date string format, like ISO8601, that can be read by the JavaScript Date.parse() method:

Time string for ISO 8601 string.kmmacros (4.2 KB)


Expand disclosure triangle to view JS source
   

    const nsDateFormatter = $.NSDateFormatter.alloc.init;

    nsDateFormatter.setDateStyle(
        $.NSDateFormatterLongStyle
    );

    nsDateFormatter.setTimeStyle(
        $.NSDateFormatterShortStyle
    );

    nsDateFormatter.setLocale(
        $.NSLocale.alloc.initWithLocaleIdentifier(
            kmvar.local_twoCharLocaleName
        )
    );


    return ObjC.unwrap(
        nsDateFormatter.stringFromDate(
            $.NSDate.alloc.initWithTimeIntervalSince1970(
                Date.parse(kmvar.local_ISO8601String) / 1000
            )
        )
    );
3 Likes

Thank you, this works great!

2 Likes

Thank you!
I’m still on Big Sur, so I have to wait until I update macOS to try this.

1 Like

Thank you!
Works fine!

1 Like

Thanks!
This doesn’t work for me.

Displayed text:
3 Machi 5877521 -596:-31

Not sure which one you are referring to –
perhaps Time string for ISO 8601 string ?

It's working here – what string did you enter for the local_ISO8601String value ?

if the same as me (2023-03-06 14:35) then there may be some side effect of the difference in our system settings.

Only experiment will reveal, I'm afraid

1 Like

I tried your string: (2023-03-06 14:35), and I got: ”3 Machi 5877521 -596:-31”.
Found out that ”sw” is language code for Swahili.
swe or sv is Swedish.
Now Date works, but there’s still problems with Time, so I have to try some more when I get the time.

2 Likes

To drop the time for the moment, you can use:

nsDateFormatter.setTimeStyle(
    $.NSDateFormatterNoStyle
);

but you may still need, as input, a ISO8601 input which includes the time zone, like:

2024-02-21T05:55:42.361Z


ISO 8601 time string with Zone.kmmacros (3.0 KB)


Localised Date string for ISO 8601 date.kmmacros (4.2 KB)


Source for testing in Script Editor, with language selector at top left set to JavaScript rather than AppleScript:

Expand disclosure triangle to view JS source
(() => {
    "use strict";

    const kmvar = {
        "local_twoCharLocaleName": "sv",
        "local_ISO8601String": (new Date()).toISOString()
    };

    const nsDateFormatter = $.NSDateFormatter.alloc.init;

    nsDateFormatter.setDateStyle(
        $.NSDateFormatterLongStyle
    );

    nsDateFormatter.setTimeStyle(
        $.NSDateFormatterNoStyle
    );

    nsDateFormatter.setLocale(
        $.NSLocale.alloc.initWithLocaleIdentifier(
            kmvar.local_twoCharLocaleName
        )
    );

    return ObjC.unwrap(
        nsDateFormatter.stringFromDate(
            $.NSDate.alloc.initWithTimeIntervalSince1970(
                Date.parse(kmvar.local_ISO8601String) / 1000
            )
        )
    );
})();
1 Like

Thanks a lot!

Thank you all!
Several solutions to this - much appreciated!

Afterthought: for some purposes it may be simpler to use Keyboard Maestro's TIME() function in (in a Calculate action, or a Calculate Token) to supply Unix Epoch seconds as a digit string.

TIME() alone for 'now'
or with more detail, like TIME(1911, 2, 23)

You can submit those digits, parsed as a JS Number(), in a pattern like this:

return ObjC.unwrap(
    nsDateFormatter.stringFromDate(
        $.NSDate.alloc.initWithTimeIntervalSince1970(
            Number(kmvar.local_UnixTimeDigits)
        )
    )
);

e.g.

Localised Date string for UNIX Time digits.kmmacros (4.7 KB)

Expand disclosure triangle to view JS source
(() => {
    "use strict";

    // Sample for testing:
    // These values can be made available, with their
    // kmvar. prefix, in the drop-down menu behind the
    // small chevron at the left of a KM11 Execute
    // JavaScript for Automation action.
    const kmvar = {
        "local_UnixTimeDigits": Application(
            "Keyboard Maestro Engine"
        )
        .calculate("TIME()"),
        "local_shortLocaleName": "sv"
    };

    const nsDateFormatter = $.NSDateFormatter.alloc.init;

    nsDateFormatter.setDateStyle(
        $.NSDateFormatterLongStyle
    );

    nsDateFormatter.setTimeStyle(
        $.NSDateFormatterNoStyle
    );

    nsDateFormatter.setLocale(
        $.NSLocale.alloc.initWithLocaleIdentifier(
            kmvar.local_shortLocaleName
        )
    );

    return ObjC.unwrap(
        nsDateFormatter.stringFromDate(
            $.NSDate.alloc.initWithTimeIntervalSince1970(
                Number(kmvar.local_UnixTimeDigits)
            )
        )
    );
})();