當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Javascript date.toTimeString()用法及代碼示例

date.toTimeString()是JavaScript中的內置函數,用於以英語返回給定日期對象的時間部分.date對象是使用date()構造函數創建的。

用法:

dateObj.toTimeString()

注意:在以上語法中,DateObj是使用Date()構造函數創建的有效Date對象,該構造函數返回其時間部分的內容。


    參數:此函數不帶任何參數。它僅與使用Date()構造函數創建的Date對象一起使用,該構造函數的時間部分內容以英語(如語言)返回。

    返回值:它以英語返回給定日期對象的時間部分。

    以下示例程序旨在說明date.toTimeString()函數:-

    <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)

    例外情況與此函數相關聯:

  1. 在這裏,創建日期對象時不傳遞任何參數作為參數,但toTimeString()函數仍返回當前時間。
    <script> 
    // Here nothing has been assigned 
    // while creating Date object 
    var dateobj = new Date(); 
      
    // It return current time using  
    // toTimeString() function. 
    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() function. 
    var B = dateobj.toTimeString(); 
      
    // Printing the time. 
    document.write(B); 
    </script>

    輸出:

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

支持的瀏覽器:JavaScript date.toTimeString()函數支持的瀏覽器如下:

  • 穀歌瀏覽器
  • IE瀏覽器
  • 火狐瀏覽器
  • Opera
  • 蘋果瀏覽器


相關用法


注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 JavaScript | date.toTimeString() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。