Date and Time Functions: Difference between revisions
No edit summary |
|||
(6 intermediate revisions by the same user not shown) | |||
Line 10: | Line 10: | ||
The Windows locale setting will affect the interpretation and formatting of date and time information. |
The Windows locale setting will affect the interpretation and formatting of date and time information. |
||
=== <span id="CompareDates">CompareDates(…)</span> === |
|||
: Compares two dates, returning a formatted elapsed period between them. |
|||
{{function description box |
|||
| name=CompareDates |
|||
| arguments=Date 1, Date 2, Mode |
|||
| description= |
|||
{{argument optional|Mode|<i>a</i>}} |
|||
The [[#CompareDates|CompareDates()]] function compares <i>Date 1</i> with <i>Date 2</i> returning the elapsed period between the two.<br> |
|||
<i>Date 1</i> or <i>Date 2</i> need to be presented in the raw format Media Center uses for all of its date related fields as detailed in the information at the top of this page. If presenting literal, human-readable dates, they must be converted using the [[ConvertDate()]] function, as shown in example #1 below. If existing Media Center date fields are to be used, then the raw date data of that field must be specified by including the <i>,0</i> instruction, like so: [Date,0]<br> |
|||
The <i>Modes</i> below can be used to format the value returned by the function. |
|||
{{argument table |
|||
| contents= |
|||
{{argument table row|a|Automatic}} |
|||
{{argument table row|c|Calendar Years}} |
|||
{{argument table row|y|Decimal Years}} |
|||
{{argument table row|d|Decimal Days}} |
|||
{{argument table row|yd|Years and Days}} |
|||
}} |
|||
'''Notes:''' |
|||
* Modes <i>a</i> and <i>c</i> return rounded results, so, for example, 37.95 years is returned as 38 years, <i>not</i> 37 years. |
|||
* Leap years are factored in automatically, though there are currently (Media Center 30.0.61) still some rounding errors that may affect your expected result. See [https://yabb.jriver.com/interact/index.php/topic,134959.msg934760.html#msg934760 this Interact Forum post] for full details. |
|||
| examples= |
|||
* '''All examples shown below use the UK date system of dd/mm/yyyy hh:mm''' |
|||
'''{{monospace|CompareDates(ConvertDate(10//03//2018 15:25),ConvertDate(11//02//2023))}}''' |
|||
: No mode is specified, so defaults to <i>a (Automatic)</i>, returning decimal years to two decimal places, <i>4.93 years</i>. Note that the forward slash in the date values needs to be escaped, hence the 'double forward slash' shown. |
|||
'''{{monospace|CompareDates([date imported<u>,0</u>],[last played<u>,0</u>])}}''' |
|||
: Any 'Date' field can be used for comparison, but the raw field data must be specified by including the <i><b>,0</b></i> instruction. |
|||
'''Maths and dates can be troublesome''' |
|||
: See [https://yabb.jriver.com/interact/index.php/topic,134959.0.html this Interact forum thread] for more information. |
|||
}} |
|||
=== <span id="ConvertDate">ConvertDate(…)</span> === |
=== <span id="ConvertDate">ConvertDate(…)</span> === |
||
: Converts a human-readable date to the internal format required for use in date fields. |
: Converts a human-readable date to the internal format required for use in date fields. |
||
Line 18: | Line 56: | ||
| style="background: #f9f9f9; color: #111; border-style: solid; border-width: 2px 2px 0 0" width="1200" | <span style="font-family: monospace,monospace; font-size:1em; color:#0f3f8d; font-size:110%"><b>convertdate(</b><i>date_time string</i><b>)</b></span> |
| style="background: #f9f9f9; color: #111; border-style: solid; border-width: 2px 2px 0 0" width="1200" | <span style="font-family: monospace,monospace; font-size:1em; color:#0f3f8d; font-size:110%"><b>convertdate(</b><i>date_time string</i><b>)</b></span> |
||
Converts a human-readable <i>date_time string</i> into the internal floating-point representation used by Media Center to store a date and time. |
Converts a human-readable <i>date_time string</i> into the internal floating-point representation used by Media Center to store a date and time. |
||
It is also possible to break the 'human-readable' <i>date_time string</i> into components and pass these to the function, like so:<br> |
|||
<b>convertdate(</b><i>year,month,day,hour,minute,second</i><b>)</b> |
|||
|- valign="top" |
|- valign="top" |
||
! scope="row" style="background: #ecedf3; color: #111; border-style: solid; border-width: 0px 1px 2px 2px; border-top: 1px solid #bbb; border-right: 1px solid #bbb;" | Examples |
! scope="row" style="background: #ecedf3; color: #111; border-style: solid; border-width: 0px 1px 2px 2px; border-top: 1px solid #bbb; border-right: 1px solid #bbb;" | Examples |
||
Line 226: | Line 267: | ||
This is accomplished by subtracting the value <span style="font-family: monospace,monospace; font-size:1em;">3</span>, which would be days, from [[#Now|Now()]], and its output formatted by [[#FormatDate|FormatDate()]].</p> |
This is accomplished by subtracting the value <span style="font-family: monospace,monospace; font-size:1em;">3</span>, which would be days, from [[#Now|Now()]], and its output formatted by [[#FormatDate|FormatDate()]].</p> |
||
|} |
|} |
||
=== <span id="PlaylistTime">PlaylistTime(…)</span> === |
|||
: Returns the time of a track in the current playlist (a sum of all previous durations) |
|||
{| style="width: 100%; border-spacing: 0; border: 0px solid black;" align="top" cellpadding="3" cellspacing="0" |
|||
|- id="PlaylistTime" valign="top" |
|||
! scope="row" style="background: #ecedf3; color: #111; border-style: solid; border-width: 2px 1px 0 2px; border-right: 1px solid #bbb;" width="100" | Description |
|||
| style="background: #f9f9f9; color: #111; border-style: solid; border-width: 2px 2px 0 0" width="1200" | <span style="font-family: monospace,monospace; font-size:1em; color:#0f3f8d; font-size:110%"><b>PlaylistTime(</b><i>Mode</i>, <i>Format</i><b>)</b></span> |
|||
The [[#PlaylistTime|PlaylistTime()]] function provides running totals of time in the current playlist. Totals returned are determined by the specified Mode value. |
|||
Argument <i>Mode</i> is optional (defaults to 0).<br> |
|||
Available <i>Mode</i> values: |
|||
<div style="margin-left: 20pt;"><table style="border-spacing:0px; border-collapse:collapse; background: #f9f9f9"> |
|||
<tr><td style="text-align:left; padding-right:20pt"><b>0</b></td><td>Returns a sum of all previous durations</td></tr> |
|||
<tr><td style="text-align:left; padding-right:20pt"><b>1</b></td><td>Returns a sum of all previous durations, including the current item</td></tr> |
|||
<tr><td style="text-align:left; padding-right:20pt"><b>2</b></td><td>Returns the remaing time of the list</td></tr> |
|||
<tr><td style="text-align:left; padding-right:20pt"><b>3</b></td><td>Returns the remaing time of the list, including the current item</td></tr> |
|||
<tr><td style="text-align:left; padding-right:20pt"><b>4</b></td><td>Returns the total playlist duration</td></tr> |
|||
</table></div> |
|||
Argument <i>Format</i> is optional (defaults to 1).<br> |
|||
Available <i>Format</i> values: |
|||
<div style="margin-left: 20pt;"><table style="border-spacing:0px; border-collapse:collapse; background: #f9f9f9"> |
|||
<tr><td style="text-align:left; padding-right:20pt"><b>0</b></td><td>Returns unformatted, raw data (seconds)</td></tr> |
|||
<tr><td style="text-align:left; padding-right:20pt"><b>1</b></td><td>Returns formatted, human readable data (time)</td></tr> |
|||
</table></div> |
|||
'''<u>NOTE:</u>'''<br> |
|||
This function's primary use will be as an expression column in a file list. Be aware that if used in a list of tens of thousands of files, the view will struggle with the calculations and become sporadically unresponsive. |
|||
|- valign="top" |
|||
! scope="row" style="background: #ecedf3; color: #111; border-style: solid; border-width: 0px 1px 2px 2px; border-top: 1px solid #bbb; border-right: 1px solid #bbb;" | Examples |
|||
|style="background: #f9f9f9; color: #111; border-style: solid; border-width: 0px 2px 2px 0; border-top: 1px solid #bbb;" | PlaylistTime() |
|||
The table below shows list expression columns using modes 0 - 4: |
|||
<div style="margin-left: 20pt;"><table style="border-spacing:0px; border-collapse:collapse; background: #f9f9f9" border="1"> |
|||
<tr><td style="text-align:left; padding-right:20pt">Name</td><td>Duration</td><td>Mode 0</td><td>Mode 1</td><td>Mode 2</td><td>Mode 3</td><td>Mode 4</td></tr> |
|||
<tr><td style="text-align:left; padding-right:20pt">Tangled Up In Blue</td><td>6:54</td><td>0:00</td><td>6:55</td><td>17:41</td><td>24:36</td><td>24:36</td></tr> |
|||
<tr><td style="text-align:left; padding-right:20pt">Simple Twist Of Fate</td><td>4:23</td><td>6:54</td><td>11:18</td><td>13:18</td><td>17:41</td><td>24:36</td></tr> |
|||
<tr><td style="text-align:left; padding-right:20pt">You're A Big Girl Now</td><td>4:24</td><td>11:18</td><td>15:43</td><td>8:53</td><td>13:18</td><td>24:36</td></tr> |
|||
<tr><td style="text-align:left; padding-right:20pt">Idiot Wind</td><td>8:53</td><td>15:43</td><td>24:36</td><td>0:00</td><td>8:53</td><td>24:36</td></tr> |
|||
</table></div> |
|||
Values in the table above have been lifted directly from a Media Center list, indicating that the function is performing some rounding up in certain stuations. |
|||
|} |
|||
<div style="text-align:right;">([[#top|Back to top)]]</div> |
<div style="text-align:right;">([[#top|Back to top)]]</div> |
Latest revision as of 09:53, 10 December 2023
- See also: Expression Language and Function Index
Media Center provides several functions for the conversion, formatting and generation of dates and times. Date and time for a Date-type field is stored internally as a single floating-point number, where the integer portion represents the number of days since the Epoch, and the decimal portion represents the fraction of a day in seconds. The Epoch is defined as December 30th, 1899 at 00:00:01. Certain fractional values and whole numbers are used to encode Time-only and Year-only values. For example, the internal Date value of "2" is considered as 1900, without a time when converted using the DateTime conversion format of FormatDate(), whereas adding a small fraction and using "2.001" instead produces a value of "1/1/1900 12:01 am". These details are only relevant if you are doing conversions.
The Windows locale setting will affect the interpretation and formatting of date and time information.
CompareDates(…)
- Compares two dates, returning a formatted elapsed period between them.
Description | CompareDates(Date 1, Date 2, Mode)
The CompareDates() function compares Date 1 with Date 2 returning the elapsed period between the two. Available mode values:
Notes:
| ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Examples | * All examples shown below use the UK date system of dd/mm/yyyy hh:mm
CompareDates(ConvertDate(10//03//2018 15:25),ConvertDate(11//02//2023))
CompareDates([date imported,0],[last played,0])
Maths and dates can be troublesome
|
ConvertDate(…)
- Converts a human-readable date to the internal format required for use in date fields.
Description | convertdate(date_time string)
Converts a human-readable date_time string into the internal floating-point representation used by Media Center to store a date and time. It is also possible to break the 'human-readable' date_time string into components and pass these to the function, like so: |
---|---|
Examples | convertdate(3//6//2012)
Returns the value 40974, which is the internal floating-point representation of the date string 3/6/2012. This value can used by any field of type Date, or in any function that requires as input a Date type value. formatdate(convertdate(May 17 1970), dddd dd MMM yyyy) Returns the value Sunday 17 May 1970, as the input does not need to be numeric, but can be in common human readable format, including "17 May 17", or "17-5-17", or "17 5 17", which all mean Wednesday 17 May 2017. Or "17 5", where the year is left out and defaults to the current year. Or "17 17", where the month is left out and default to the current month. formatdate(convertdate(12//2//1985), decade)) Converts the date string 12/2/1985 (note: December 2nd, not February 12th, though the function appears to follow system date formatting) into a Date type value, and then formats the result as a decades grouping, returning 1980's. This might be used for creating decade groupings. |
DateInRange(…)
- Functions as a switch or select case statement.
Description | DateInRange(Date, Range Start, Range End)
The DateInRange() function compares a date against a range of dates, returning "1" if the date is within the given range, and "0" if it is not. Notes:
Related: See also IsRange(…) |
---|---|
Examples | * All examples shown below use the UK date system of dd/mm/yyyy hh:mm
DateInRange([Date],1980,1990)
DateInRange([Date],01//03//2012,31//03//2012)
DateInRange([Date],01-03-2012,31-03-2012)
if(DateInRange([Date],17-03-2012 00:00,17-03-2012 12:00),The morning of March 17th 2012,Some other time)
|
FormatDate(…)
- Formats a date value in a specified manner.
Description | formatdate(date value, format specifier, empty label)
The FormatDate() function provides custom formatting of date and time values through the use of a format specifier. Output will be formatted according to format specifier. The date value is a Media Center internal floating-point date/time representation, stored in Date fields, and output from various functions such as Now() and ConvertDate(). To pass a field of type Date to FormatDate(), use the raw (unformatted) field specification, such as "[Date Imported,0]". The format specifier provides a recipe for converting the internal value into a human-readable string. Supported are a variety of Windows style, strftime() style, and Media Center-specific formats specifiers. Construct the format specifier from any number or combination of those defined in the following table. Additionally, any non-format characters will be output without interpretation. This allows creating rich date/time output strings. To output a word that is a reserved format specifier, surround the word with double-quotes. The empty label argument will be output if the date value is empty.
Argument date value is optional (defaults to [date,0]). Argument empty label is optional (defaults to EMPTY). | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Examples | formatdate(year-month)
Outputs the file's date formatted as Year-Month, such as 2012-April. The default date value of [Date,0] is used. formatdate([last played,0], yyyy//MM//dd, Not Yet) Returns the file's last played date as year/month/day without the time, ignoring the system locale setting. If a file has no last played value, the expression will output Not Yet instead. formatdate([date modified,0], month %Y) Returns the file's modification date/time in the form of a long month name and a four-digit year, such as December 2010. formatdate([date imported,0], The "year" is year) Outputs the The year is ####, where #### is the year the file was imported into the Library. Note that the word year must be surrounded in double-quotes to have it considered as literal text, and not the Year format specifier. formatdate([date imported,0], month)&datatype=[month] This examples is the same as the previous example, but includes a cast to the Month type &datatype=[month]. This cast can be used to cause chronological month-sorting, rather than month name alphabetic-sorting, in a panes or category view. Data-type coercion is discussed above. Additional Examples |
Now(…)
- Retrieve and display the system date.
Description | now()
The Now() function returns a floating-point value representing the current system date and time. It is generally useful for performing date arithmatic in expressions that desire to figure out elapsed time. Any raw date field or value representing a date can be subtracted from Now() to realize an elapsed time delta. |
---|---|
Examples | now()
When run on Aug 17, 2013 at 19:28:00, returns approximately 41503.811115995. formatdate(now(), date) Returns the current date, without a time component, formatted according to the system's locale settings. formatdate(math(now() - 3, dddd dd MMMM yyyy H:mm) The date from three days ago is formatted as something like Wednesday 14 August 2013 19:35. This is accomplished by subtracting the value 3, which would be days, from Now(), and its output formatted by FormatDate(). |
PlaylistTime(…)
- Returns the time of a track in the current playlist (a sum of all previous durations)
Description | PlaylistTime(Mode, Format)
The PlaylistTime() function provides running totals of time in the current playlist. Totals returned are determined by the specified Mode value. Argument Mode is optional (defaults to 0).
Argument Format is optional (defaults to 1).
NOTE: | |||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Examples | PlaylistTime()
The table below shows list expression columns using modes 0 - 4:
Values in the table above have been lifted directly from a Media Center list, indicating that the function is performing some rounding up in certain stuations. |