5
<title>Example: DataType.Date.format()</title>
6
<link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
7
<link rel="stylesheet" href="../assets/css/main.css">
8
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
9
<script src="../../build/yui/yui-min.js"></script>
14
<h1>Example: DataType.Date.format()</h1>
19
<div id="main" class="yui3-u">
20
<div class="content"><style scoped>
21
/* custom styles for this example */
22
#demo .output {margin-bottom:1em; padding:10px; border:1px solid #D9D9D9;}
26
<p>The Date module of the DataType Utility allows you to format a JavaScript Date object into a string with <code>strftime</code> syntax. This method allows you to specify any tokenized HTML value as the format. However, in this example the formatted output will be escaped for security.</p>
28
<p><strong>NOTE:</strong> As of 3.1.0, the formatting patterns used by DateType.Date can be driven by external language resource bundles as described in the <a href="datatype-dateformat-lang.html">"Formatting Dates Using Language Resource Bundles" Example</a>. This examples currently uses the 3.0.0 <code>dateFormat</code> and <code>locale</code> instance configuration support which have been deprecated.</p>
31
<div class="example yui3-skin-sam">
34
<label for="demo_format">Format (optional):</label>
35
<input type="text" id="demo_format" value="%r">
36
<label for="demo_locale">Locale (optional):</label>
37
<input type="text" id="demo_locale" value="en-GB">
38
<input type="button" id="demo_btn" value="Format current date =>">
39
<div id="demo_output" class="output"> </div>
46
YUI().use("node", "datatype-date", "escape", function (Y) {
47
Y.on("click", function(e){
48
var date = new Date(),
49
output = Y.DataType.Date.format(date, {format:Y.one("#demo_format").get("value"), locale:Y.one("#demo_locale").get("value")});
52
output += "(Invalid input)"
54
Y.one("#demo_output").setContent(Y.Escape.html(output));
61
<p>To convert a data value to a date, simply call the <code>parse()</code> function of the DataType.Date class:</p>
63
<pre class="code prettyprint">YUI().use("datatype-date", function(Y) {
64
var date = Y.DataType.Date.parse("Jan 7, 2003");
65
// date is a JavaScript Date object
69
<p>Under the hood, the data value is converted to a date via the <code>Date()</code> constructor:</p>
71
<pre class="code prettyprint">YUI().use("datatype-date", function(Y) {
72
// These all return dates
73
var date = Y.DataType.Date.parse("December 17, 1995 03:24:00");
75
date = Y.DataType.Date.parse(1995,11,17);
77
date = Y.DataType.Date.parse(1995,11,17,3,24,0);
79
date = Y.DataType.Date.parse(948548583);
85
<div id="sidebar" class="yui3-u">
91
<h2 class="no-toc">Examples</h2>
98
<li data-description="Formatting dates into strings.">
99
<a href="datatype-dateformat.html">DataType.Date.format()</a>
104
<li data-description="Formatting dates into strings using pre-packaged language resource bundles.">
105
<a href="datatype-dateformat-lang.html">Formatting Dates Using Language Resource Bundles</a>
110
<li data-description="Parsing data into dates.">
111
<a href="datatype-dateparse.html">DataType.Date.parse()</a>
116
<li data-description="Formatting numbers into strings.">
117
<a href="datatype-numberformat.html">DataType.Number.format()</a>
122
<li data-description="Parsing data into numbers.">
123
<a href="datatype-numberparse.html">DataType.Number.parse()</a>
128
<li data-description="Formatting XML documents into strings.">
129
<a href="datatype-xmlformat.html">DataType.XML.format()</a>
134
<li data-description="Parsing strings into XML documents.">
135
<a href="datatype-xmlparse.html">DataType.XML.parse()</a>
149
<script src="../assets/vendor/prettify/prettify-min.js"></script>
150
<script>prettyPrint();</script>