CoffeeScript 日期 - toLocaleString()



说明

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

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

语法

下面是 toLocaleString() 方法的语法。

Date.toLocaleString()

返回值

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

示例

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

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

打开 命令提示符 并按照如下所示编译 .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);

现在,再次打开 命令提示符,并按如下所示运行 CoffeeScript 文件。

c:\> coffee date_tolocalestring.coffee

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

7/28/1993, 2:39:07 PM
coffeescript_date.htm
广告
© . All rights reserved.