Date.prototype.toString()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Date/toString
  • Revision title: Date.prototype.toString()
  • Revision id: 487109
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment cleanup

Revision Content

{{JSRef("Global_Objects", "Date")}}

Summary

The toString() method returns a string representing the specified Date object.

Syntax

dateObj.toString()

Parameters

None.

Description

The {{jsxref("Global_Object/Date", "Date")}} object overrides the toString method of the {{jsxref("Global_Object/Object", "Object")}} object; it does not inherit {{jsxref("Object.prototype.toString()")}}. For {{jsxref("Global_Object/Date", "Date")}} objects, the toString method returns a string representation of the object.

toString always returns a string representation of the date in American English.

JavaScript calls the toString method automatically when a date is to be represented as a text value or when a date is referred to in a string concatenation.

Examples

Example: Using toString

The following assigns the toString value of a Date object to myVar:

var x = new Date();
myVar = x.toString(); // assigns a value to myVar similar to:
// Mon Sep 28 1998 14:36:22 GMT-0700 (Pacific Daylight Time)

Specifications

Specification Status Comment
ECMAScript 1st Edition. Implemented in JavaScript 1.0 Standard Initial definition.
{{SpecName('ES5.1', '#sec-15.9.5.2', 'Date.prototype.toLocaleTimeString')}} {{Spec2('ES5.1')}}  
{{SpecName('ES6', '#sec-date.prototype.tostring', 'Date.prototype.toString')}} {{Spec2('ES6')}}  

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}

See also

  • {{jsxref("Object.prototype.toString()")}}
  • {{jsxref("Date.prototype.toDateString()")}}
  • {{jsxref("Date.prototype.toLocaleString()")}}
  • {{jsxref("Date.prototype.toTimeString()")}}

Revision Source

<div>
 {{JSRef("Global_Objects", "Date")}}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>The <code><strong>toString()</strong></code> method returns a string representing the specified Date object.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<var>dateObj</var>.toString()</pre>
<h3 id="Parameters" name="Parameters">Parameters</h3>
<p>None.</p>
<h2 id="Description" name="Description">Description</h2>
<p>The {{jsxref("Global_Object/Date", "Date")}} object overrides the <code>toString</code> method of the {{jsxref("Global_Object/Object", "Object")}} object; it does not inherit {{jsxref("Object.prototype.toString()")}}. For<code> </code>{{jsxref("Global_Object/Date", "Date")}} objects, the <code>toString</code> method returns a string representation of the object.</p>
<p><code>toString</code> always returns a string representation of the date in American English.</p>
<p>JavaScript calls the <code>toString</code> method automatically when a date is to be represented as a text value or when a date is referred to in a string concatenation.</p>
<h2 id="Examples" name="Examples">Examples</h2>
<h3 id="Example:_Using_toString" name="Example:_Using_toString">Example: Using <code>toString</code></h3>
<p>The following assigns the <code>toString</code> value of a <code>Date</code> object to <code>myVar</code>:</p>
<pre class="brush:js">
var x = new Date();
myVar = x.toString(); // assigns a value to myVar similar to:
// Mon Sep 28 1998 14:36:22 GMT-0700 (Pacific Daylight Time)
</pre>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>ECMAScript 1st Edition. Implemented in JavaScript 1.0</td>
   <td>Standard</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.9.5.2', 'Date.prototype.toLocaleTimeString')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-date.prototype.tostring', 'Date.prototype.toString')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Chrome</th>
    <th>Firefox (Gecko)</th>
    <th>Internet Explorer</th>
    <th>Opera</th>
    <th>Safari</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
   </tr>
  </tbody>
 </table>
</div>
<div id="compat-mobile">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Android</th>
    <th>Chrome for Android</th>
    <th>Firefox Mobile (Gecko)</th>
    <th>IE Mobile</th>
    <th>Opera Mobile</th>
    <th>Safari Mobile</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
   </tr>
  </tbody>
 </table>
</div>
<h2 id="See_Also" name="See_Also">See also</h2>
<ul>
 <li>{{jsxref("Object.prototype.toString()")}}</li>
 <li>{{jsxref("Date.prototype.toDateString()")}}</li>
 <li>{{jsxref("Date.prototype.toLocaleString()")}}</li>
 <li>{{jsxref("Date.prototype.toTimeString()")}}</li>
</ul>
Revert to this revision