This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
Dates_and_Times [2016/04/20 08:26] AlainLifchitz add internal links |
Dates_and_Times [2017/06/09 19:09] JMichaelTX Add example using KM Variables |
||
---|---|---|---|
Line 7: | Line 7: | ||
===== Examples ===== | ===== Examples ===== | ||
+ | ==== 1. Get Current Date/Time ==== | ||
+ | |||
The most commonly used token is for returning the current date/time in the format you like: | The most commonly used token is for returning the current date/time in the format you like: | ||
Line 14: | Line 16: | ||
For example, using the [[action:Set_Variable_to_Text|Set Variable to Text]] Action:\\ | For example, using the [[action:Set_Variable_to_Text|Set Variable to Text]] Action:\\ | ||
{{:km7-set-var-to-date.png|}} | {{:km7-set-var-to-date.png|}} | ||
+ | |||
+ | ==== 2. Add Time to Date ==== | ||
+ | This example adds days to the current date using a Keyboard Maestro Variable. See below for other options.{{:add-days-current-date-km-7.3.png?nolink|}} | ||
+ | |||
+ | For more info see this Forum post: [[https://forum.keyboardmaestro.com/t/enter-todays-date-a-certain-number-of-days/7211/2?u=jmichaeltx | Instructions to Setup Macro to Add Days to Date]] | ||
+ | |||
+ | --- | ||
+ | |||
Line 21: | Line 31: | ||
^Usage^Token and Format^Results^ | ^Usage^Token and Format^Results^ | ||
- | |International|%ICUDateTime%yyyy/MM/dd%|2015/11/28| | + | |[[http://www.iso.org/iso/home/standards/iso8601.htm | ISO/International]]|%ICUDateTime%yyyy-MM-dd%|2015-11-28| |
|Australia\\ Europe|%ICUDateTime%dd/MM/yyyy%|28/11/2015| | |Australia\\ Europe|%ICUDateTime%dd/MM/yyyy%|28/11/2015| | ||
|United States|%ICUDateTime%EEE, MMM d, yyyy%|Sat, Nov 28, 2015| | |United States|%ICUDateTime%EEE, MMM d, yyyy%|Sat, Nov 28, 2015| | ||
Line 31: | Line 41: | ||
###Format Codes | ###Format Codes | ||
- | Here are some commonly used format codes. See the [ICU Date/Time codes](http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Time-Format-Syntax) for the full list. | + | **For the complete list** of codes, see the [ICU Date/Time codes](http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Time-Format-Syntax). |
+ | **Some Commonly Used Format Codes** | ||
^Period^ Format\\ Code ^Example^Results^ | ^Period^ Format\\ Code ^Example^Results^ | ||
^Year| y((Do not use capital Y (year of "Week of Year")!)) |yy\\ yyyy|96\\ 1996| | ^Year| y((Do not use capital Y (year of "Week of Year")!)) |yy\\ yyyy|96\\ 1996| | ||
Line 44: | Line 55: | ||
^Minute| m |m\\ mm|4\\ 04| | ^Minute| m |m\\ mm|4\\ 04| | ||
^Second| s |s\\ ss|5\\ 05| | ^Second| s |s\\ ss|5\\ 05| | ||
+ | ^Fractional Second\\ (truncates)| S |S\\ SSS|1\\ 123| | ||
(!) Do not use capital "Y" (year of "Week of Year") unless you really mean it or you will get oddly strange results at the beginning/end of each year. | (!) Do not use capital "Y" (year of "Week of Year") unless you really mean it or you will get oddly strange results at the beginning/end of each year. | ||
- | ===== Date/Time Tokens ===== | + | |
+ | ===== Date/Time Tokens & Formulas ===== | ||
Also see Wiki article [[token:ICUDateTime]] token | Also see Wiki article [[token:ICUDateTime]] token | ||
- | ^KM Token^Example^ | + | ^Date/Time Token/Formula^Example^ |
|**//%ICUDateTime%<format>%//**\\ Returns Current Date/Time (local)|Get Current Date/Time\\ %ICUDateTime%EEE, MMM d, yyyy%\\ => Sat, Nov 28, 2015| | |**//%ICUDateTime%<format>%//**\\ Returns Current Date/Time (local)|Get Current Date/Time\\ %ICUDateTime%EEE, MMM d, yyyy%\\ => Sat, Nov 28, 2015| | ||
|**//%ICUDateTimePlus%<Quantity>%<TimeUnit>%<format>%//**\\ Adds to Current Date/Time (local)|Add 3 days\\ %ICUDateTimePlus%**3%Days**%EEE, MMM d, yyyy%\\ => Tue, Dec 1, 2015| | |**//%ICUDateTimePlus%<Quantity>%<TimeUnit>%<format>%//**\\ Adds to Current Date/Time (local)|Add 3 days\\ %ICUDateTimePlus%**3%Days**%EEE, MMM d, yyyy%\\ => Tue, Dec 1, 2015| | ||
Line 55: | Line 69: | ||
|**//%ICUDateTimeFor%NOW()<+ or –\><Seconds>%format%//**\\ Adds or Subtracts from Current Date/Time (GMT)|Add 1 hour to Current GMT\\ %ICUDateTimeFor%NOW()+ **(60\*60)** %EEE, MMM d, yyyy h:mm%\\ => Sat, Nov 28, 2015 7:14| | |**//%ICUDateTimeFor%NOW()<+ or –\><Seconds>%format%//**\\ Adds or Subtracts from Current Date/Time (GMT)|Add 1 hour to Current GMT\\ %ICUDateTimeFor%NOW()+ **(60\*60)** %EEE, MMM d, yyyy h:mm%\\ => Sat, Nov 28, 2015 7:14| | ||
|**//%ICUDateTimeFor% TIME(<Year>, <Month>, <Day> [, <Hour>, <Min>, Sec>])%<format>%//**\\ Sets absolute date/time (GMT)|Set absolute date in **GMT**\\ %ICUDateTimeFor% TIME(2015, 12, 25) %EEE, MMM d, yyyy%\\ => Thu, Dec 24, 2015\\ \\ Set absolute date in **LOCAL** time\\ %ICUDateTimeFor% TIME(2015, 12, 25) **- GMTOFFSET()** %EEE, MMM d, yyyy%\\ => Fri, Dec 25, 2015| | |**//%ICUDateTimeFor% TIME(<Year>, <Month>, <Day> [, <Hour>, <Min>, Sec>])%<format>%//**\\ Sets absolute date/time (GMT)|Set absolute date in **GMT**\\ %ICUDateTimeFor% TIME(2015, 12, 25) %EEE, MMM d, yyyy%\\ => Thu, Dec 24, 2015\\ \\ Set absolute date in **LOCAL** time\\ %ICUDateTimeFor% TIME(2015, 12, 25) **- GMTOFFSET()** %EEE, MMM d, yyyy%\\ => Fri, Dec 25, 2015| | ||
+ | |**//%ICUDateTimePlus%<Formula for Quantity>%<TimeUnit>%<format>%//**\\ Using functions to calculate date offset.|**Get Date for Selected Prior Day Number**\\ `%ICUDateTimePlus%-(DOW() + 7 - DayNum)%Days%EEE, MMM d, yyyy%`\\ If Current Date is Dec 25, 2016, and _DayNum_ is 1:\\ => Sun, Dec 18, 2016\\ where "DayNum" is the number of the weekday (can be a Variable)| | ||
+ | |||
*where* | *where* | ||
Line 67: | Line 83: | ||
* *<Year>, <Month>, <Day>, <Hour>, <Min>, Sec> are in integer units for the indicated period.* | * *<Year>, <Month>, <Day>, <Hour>, <Min>, Sec> are in integer units for the indicated period.* | ||
- | Since [[function:TIME|TIME()]] and [[function:NOW|NOW()]] returns the specified date/time in GMT, you can adjust for your local time zone by subtracting the [[function:GMTOFFSET|GMTOFFSET()]] function | + | Since [[function:TIME|TIME()]] and [[function:NOW|NOW()]] returns the specified date/time in GMT :!: , you can adjust for your local time zone by subtracting the [[function:GMTOFFSET|GMTOFFSET()]] function |
- | %ICUDateTimeFor% TIME(2015, 12, 25) - GMTOFFSET() %EEE, MMM d, yyyy%\\ | + | :!: FIXME -- this has been [[https://forum.keyboardmaestro.com/t/issue-with-date-time-functions/5856 | disputed]]. `NOW()` and `TIME()` may be based on local time. |
+ | |||
+ | [[token:ICUDateTimeFor|%ICUDateTimeFor%]] [[function:TIME|TIME(2015, 12, 25)]] - [[function:GMTOFFSET|GMTOFFSET()]] %EEE, MMM d, yyyy%\\ | ||
=> Fri, Dec 25, 2015 | => Fri, Dec 25, 2015 | ||
+ | |||
===== Date/Time Functions and Calculations ===== | ===== Date/Time Functions and Calculations ===== | ||
Line 79: | Line 98: | ||
* [[function:DAY|DAY() (function)]] | * [[function:DAY|DAY() (function)]] | ||
* [[function:DOW|DOW() (function)]] | * [[function:DOW|DOW() (function)]] | ||
+ | * [[function:GMTOFFSET|GMTOFFSET() (function)]] | ||
* [[function:JD2TIME|JD2TIME() (function)]] | * [[function:JD2TIME|JD2TIME() (function)]] | ||
* [[function:MJD2TIME|MJD2TIME() (function)]] | * [[function:MJD2TIME|MJD2TIME() (function)]] | ||
Line 96: | Line 116: | ||
* [[JulianDate|JulianDate]] | * [[JulianDate|JulianDate]] | ||
* [[unixtime]] in GMT | * [[unixtime]] in GMT | ||
+ | |||
+ | ==== Mixing Functions and Tokens ==== | ||
+ | |||
+ | You might expect [[token:ICUDateTimeFor|%ICUDateTimeFor%]] [[function:TIME|TIME(2017, 1, 1)]] `%EEE, MMM d, yyyy%` to yield `Sun, Jan 1, 2017`.\\ | ||
+ | However, it may yield `Sat, Dec 31, 2016`, depending on your local time zone. | ||
+ | |||
+ | |||
+ | Here is why: | ||
+ | |||
+ | * The [[function:TIME|TIME()]] function takes and returns the values in GMT. | ||
+ | * The [[token:ICUDateTimeFor|%ICUDateTimeFor%]] token displays the localized time. | ||
+ | * Thus, for [[function:TIME|TIME(Year, Month, Day)]] (which is in GMT), the ICUDateTimeFor displayed date in local time will be one less for anyone with a negative time zone offset. | ||
+ | * It is best is to use [[function:TIME|TIME(Year, Month, Day, 12, 0, 0)]] which will give noon GMT time, and thus the same day in local time for pretty much everyone in the world (everyone with time zone differences strictly between -12 hours and +12 hours). | ||
+ | |||
+ | So, if you use [[token:ICUDateTimeFor|%ICUDateTimeFor%]] [[function:TIME|TIME(2017, 1, 1 , 12, 0, 0)]] `%EEE, MMM d, yyyy%` you will now get the expected result of `Sun, Jan 1, 2017` | ||
+ | |||
===== Discussion on Keyboard Maestro Forum ===== | ===== Discussion on Keyboard Maestro Forum ===== | ||
Line 114: | Line 150: | ||
==== Forum ==== | ==== Forum ==== | ||
+ | - [KM dates & times: Notes on calculations, anchors, intervals, and translations](https://forum.keyboardmaestro.com/t/km-dates-times-notes-on-anchors-intervals-and-translations/1935) (Aug 2015) | ||
+ | - [[https://forum.keyboardmaestro.com/t/date-calculations/2165|Date Calculations]] | ||
- [[https://forum.keyboardmaestro.com/t/previous-date-variables-best-way/3311/4|Previous Date Variables]] | - [[https://forum.keyboardmaestro.com/t/previous-date-variables-best-way/3311/4|Previous Date Variables]] | ||
- [[https://forum.keyboardmaestro.com/t/icudatetime-format-from-locale/3458/5|%ICUDateTime% format from locale]] | - [[https://forum.keyboardmaestro.com/t/icudatetime-format-from-locale/3458/5|%ICUDateTime% format from locale]] | ||
- | - [[https://www.google.fr/search?q=site:forum.keyboardmaestro.com+Dates+Times|Keyboard Maestro Forum topics about Dates and Times]] | + | - [[https://forum.keyboardmaestro.com/tags/date_time|Keyboard Maestro Forum topics about Dates and Times]] |
==== Related Pages ==== | ==== Related Pages ==== | ||