JavaScript Date to timestring()方法

下面是一个例子 日期到时间字符串() 方法

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
    // of time portion is returned
    // using toTimeString() function.
    var B = dateobj.toTimeString();
    // Printing the time.
    document.write(B);
    </script>

    
    

  • 输出:
    05:35:32 GMT+0530 (India Standard Time)

这个 日期toTimeString() 方法用于以英语返回给定日期对象的时间部分。date对象是使用date()构造函数创建的。

语法:

dateObj.toTimeString()

参数: 此函数不接受任何参数。它只是与使用Date()构造函数创建的日期对象一起使用,该构造函数的时间部分内容以类似英语的语言返回。

返回值: 它返回给定日期对象的英语时间部分。

注: 这个 DateObj 是使用Date()构造函数创建的有效日期对象,返回其时间部分内容。

以上方法的更多代码如下:

项目1: 在这里,在创建date对象时没有传递任何参数,但toTimeString()方法仍返回当前时间。

<script>
// Here nothing has been assigned
// while creating Date object
var dateobj = new Date();
// It return current time using
// toTimeString() method.
var B = dateobj.toTimeString();
// Printing the current time.
document.write(B);
</script>


输出:

14:58:08 GMT+0530 (India Standard Time)

项目2: 如果Date()构造函数的参数没有指定时间部分,那么它会将时间返回为零(0)。

<script>
// Only month date and year are
// assigned without time while
// creating Date object.
var dateobj = new Date( 'October 15, 1996' );
// It returns time using
// toTimeString() method.
var B = dateobj.toTimeString();
// Printing the time.
document.write(B);
</script>


输出:

00:00:00 GMT+0530 (India Standard Time)

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

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