JavaScript Date toString()方法

下面是一个例子 Date toString() 方法

null
  • 例子:

    <script>
    // Here a date has been assigned
    // while creating Date object
    var dateobj =
    new Date( 'October 15, 1996 05:35:32' );
    // Contents of above date object is converted
    // into a string using toString() function.
    var B = dateobj.toString();
    // Printing the converted string.
    document.write(B);
    </script>

    
    

  • 输出:
    Tue Oct 15 1996 05:35:32 GMT+0530 (India Standard Time)

这个 日期toString() 方法用于将给定日期对象的内容转换为字符串。date对象是使用date()构造函数创建的。 语法:

dateObj.toString()

参数: 此方法不接受任何参数。它只是与使用Date()构造函数创建的日期对象一起使用,该构造函数的内容被转换为字符串。

返回值: 它返回转换后的字符串。

注: 这个 DateObj 是使用Date()constructor创建的有效日期对象,其内容转换为字符串。

以上方法的更多代码如下: 项目1: 在这里,创建date对象时不会传递任何参数,但toString()方法仍会返回当前日期、月份、日期、年份和时间。

<script>
// Here nothing has been assigned
// while creating Date object
var dateobj = new Date();
// Contents of above date object
// is converted into a string
// using toString() method.
var B = dateobj.toString();
// Printing the converted string.
document.write(B);
</script>


输出:

Wed Apr 11 2018 00:50:44 GMT+0530 (India Standard Time)

项目2: 当传递一些随机值列表时,toString()方法返回相应的生成字符串。

Date()构造函数的格式类似于Date(月、日、年、时间)。通过遵循这种格式,以下程序中给出了一些值,并生成相应的字符串作为输出。时间格式应该类似于(number:number:number)。如果时间不是这种格式,它将输出为无效日期。

<script>
// Here some different values has been
// assigned while creating Date object
var dateobj1 = new Date( '1' );
var dateobj2 = new Date( '2, 3' );
var dateobj3 = new Date( '4, 5, 6' );
var dateobj4 = new Date( '7, 8, 3, 4' );
var dateobj5 = new Date( '4, 5, 6, 11:00:12' );
var dateobj6 = new Date( '12, 5, 4, 0:0' );
// Contents of above date objects is converted
// into strings using toString() method.
var B = dateobj1.toString();
var C = dateobj2.toString();
var D = dateobj3.toString();
var E = dateobj4.toString();
var F = dateobj5.toString();
var G = dateobj6.toString();
// Printing the converted string.
document.write(B + "<br>" );
document.write(C + "<br>" );
document.write(D + "<br>" );
document.write(E + "<br>" );
document.write(F + "<br>" );
document.write(G);
</script>


输出:

Mon Jan 01 2001 00:00:00 GMT+0530 (India Standard Time)
Sat Feb 03 2001 00:00:00 GMT+0530 (India Standard Time)
Wed Apr 05 2006 00:00:00 GMT+0530 (India Standard Time)
Invalid Date"
Wed Apr 05 2006 11:00:12 GMT+0530 (India Standard Time)
Sun Dec 05 2004 00:00:00 GMT+0530 (India Standard Time)

支持的浏览器: 支持的浏览器 JavaScript Date toString()方法 以下列出了:

  • 谷歌Chrome 1及以上版本
  • 边缘12及以上
  • Firefox 1及以上版本
  • Internet Explorer 3及以上版本
  • 歌剧3及以上
  • Safari 1及以上
© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享