format函数)参数用法介绍

  moment().format()函数用于根据用户需要格式化日期。该格式可以字符串形式提供js中的format函数,并将其作为参数传递给此函数。

  用法:

  <pre>moment().format(String);</pre>

  参数:此函数接受字符串类型的单个参数,该参数定义了格式。

  返回值:此函数返回日期。

  力矩模块的安装:

  您可以访问“安装力矩”模块的链接。您可以使用此命令安装此软件包。

  <pre>npm install moment</pre>

  安装力矩模块后,可以使用命令在命令提示符下检查力矩版本。

  <pre>npm version moment</pre>

  之后js中的format函数,您可以创建一个文件夹并添加一个文件,例如index.js,如下所示

  范例1: 文件名:index.js

   // Requiring the module

    const moment = require('moment'); 
       
    // The format() function to format the date  
    var formatedDate = moment().format( 
        "dddd, MMMM Do YYYY, h:mm:ss a"); 

  运行程序的步骤:

  项目结构将如下所示:

  js date的format函数_js date.format函数_js中的format函数

  使用以下命令运行index.js文件:

  <pre>node index.js</pre>

  输出:

  <pre>
Friday, July 17th 2020, 4:28:30 pm
</pre>

  范例2: 文件名:index.js

   // Requiring the module

    const moment = require('moment'); 
       
    function format_Date(date){ 
       return moment().format("dddd, MMMM Do YYYY"); 
    } 
       
    var result = format_Date(moment); 

  运行程序的步骤:

  项目结构将如下所示:

  js date的format函数_js date.format函数_js中的format函数

  使用以下命令运行index.js文件:

  <pre>node index.js</pre>

  输出:

  <pre>
Result:Friday, July 17th 2020
</pre>

  参考: #/displaying/format/

  相关用法

文章由官网发布,如若转载,请注明出处:https://www.veimoz.com/1840
0 评论
364

发表评论

!