目录

toLocaleString()

描述 (Description)

toLocaleString()方法使用操作系统的本地约定将日期转换为字符串。

toLocaleString方法依赖于格式化日期的底层操作系统。 它使用运行脚本的操作系统的格式约定将日期转换为字符串。 例如,在美国,月份出现在日期(04/15/98)之前,而在德国,日期出现在月份之前(15.04.98)。

语法 (Syntax)

下面给出了toLocaleString()方法的语法。

Date.toLocaleString()

返回值 (Return Value)

以字符串格式返回格式化日期。

例子 (Example)

以下示例演示了CoffeeScript中toLocaleString()方法的用法。 将此代码保存在名为date_tolocalestring.coffee的文件中。

dt = new Date(1993, 6, 28, 14, 39, 7);
console.log dt.toLocaleString()

打开command prompt并编译.coffee文件,如下所示。

c:\> coffee -c date_tolocalestring.coffee

在编译时,它为您提供以下JavaScript。

// Generated by CoffeeScript 1.10.0
(function() {
  var dt;
  dt = new Date(1993, 6, 28, 14, 39, 7);
  console.log(dt.toLocaleString());
}).call(this);

现在,再次打开command prompt ,然后运行CoffeeScript文件,如下所示。

c:\> coffee date_tolocalestring.coffee

执行时,CoffeeScript文件生成以下输出。

7/28/1993, 2:39:07 PM
↑回到顶部↑
WIKI教程 @2018