date-and-time.Date.preparse()是用于操作 JS 日期和时间模块的极简函数集合,用于根据某种模式预先解析日期。
所需模块:通过 npm 安装模块或在本地使用它。
- 通过使用 npm。
 
npm install date-and-time --save
- 通过使用 CDN 链接。
 
<script src="/path/to/date-and-time.min.js"></script>
用法:
preparse(dateString, arg)
参数:此方法采用以下参数作为参数:
- dateString:它是日期的字符串对象。
 - arg:这是所需的日期格式。
 
返回值:此方法返回解析的日期和时间。
范例1:
index.js
// Node.js program to demonstrate the  
// Date.preparse() method
  
// Importing module
const date = require('date-and-time')
  
// Creating object of current date and time 
// by using Date() 
const now  =  new Date('07-03-2021');
  
// Pre parsing the date and time
// by using date.preparse() method
const value = date.preparse(now.toLocaleDateString(),'D/M/YYYY');
  
// Display the result
console.log(value)使用以下命令运行index.js文件:
node index.js
输出:
{
  Y:2021,   
  M:3,      
  D:7,      
  H:0,      
  A:0,      
  h:0,      
  m:0,      
  s:0,      
  S:0,      
  Z:0,      
  _index:8, 
  _length:8,
  _match:3  
}范例2:
index.js
// Node.js program to demonstrate the  
// Date.preparse() method
  
// Importing module
const date = require('date-and-time')
  
// Pre parsing the date and time
// by using date.preparse() method
const value = date.preparse('23:14:05 GMT+0900','HH:mm:ss [GMT]Z');
  
// Display the result
console.log("pre parsed date and time:" + value._length)使用以下命令运行index.js文件:
node index.js
输出:
pre parsed date and time:17
参考:https://github.com/knowledgecode/date-and-time
相关用法
- Node.js GM drawLine()用法及代码示例
 - Node.js GM drawArc()用法及代码示例
 - Node.js GM drawPolyline()用法及代码示例
 - Node.js GM drawBezier()用法及代码示例
 - Node.js GM drawCircle()用法及代码示例
 - Node.js GM drawEllipse()用法及代码示例
 
注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 Node.js Date.preparse() API。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
