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


Javascript Intl.DateTimeFormat.prototype.format()用法及代碼示例

Intl.DateTimeFormat.prototype.format() 方法是 JavaScript 中的內置方法,用於根據此 Intl.DateTimeFormat 對象的語言環境和格式選項來格式化日期。

用法:

dateTimeFormat.format( date )

參數:此方法接受如上所述和以下描述的單個參數:

  • date:該參數保存需要格式化的日期。

下麵的例子說明了 JvaaScript 中的 Intl.DateTimeFormat.prototype.format() 方法:

範例1:




const Geeks = { weekday:'long', year: 
'numeric', month:'long', day:'numeric' };
const dateformat = new Date(1997, 06, 30);
   
const dateTimeFormat4 = new Intl.DateTimeFormat('hi', Geeks);
console.log(dateTimeFormat4.format(dateformat));
   
const dateTimeFormat2 = new Intl.DateTimeFormat('en-GB', Geeks);
console.log(dateTimeFormat2.format(dateformat));
   
const dateTimeFormat1 = new Intl.DateTimeFormat('sr-RS', Geeks);
console.log(dateTimeFormat1.format(dateformat));
   
const dateTimeFormat3 = new Intl.DateTimeFormat('en-US', Geeks);
console.log(dateTimeFormat3.format(dateformat));

輸出:

範例2:


var list = [new Date(2012, 08), new Date(2012, 11),
            new Date(2012, 03)];
var geeks = { year:'numeric', month:'long' };
  
var dateTime = new Intl.DateTimeFormat('hi', geeks);
var result = list.map(dateTime.format);
console.log(result.join(' <-> '));
  
var dateTime1 = new Intl.DateTimeFormat('tr', geeks);
var result1 = list.map(dateTime1.format);
console.log(result1.join(' ; '));
  
var dateTime2 = new Intl.DateTimeFormat('LT', geeks);
var result2 = list.map(dateTime2.format);
console.log(result2.join('::'));

輸出:

支持的瀏覽器: Intl.DateTimeFormat.prototype.format() 方法支持的瀏覽器如下:

  • 穀歌瀏覽器
  • Firefox
  • Opera
  • Edge
  • IE
  • Safari



相關用法


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