简体   繁体   English

将Moment.JS与Chance.JS结合使用

[英]Using Moment.JS with Chance.JS

For a project I'm currently using chance.js to generate some random data that I will put inside an array that I will use later. 对于一个项目,我目前正在使用偶然性.js生成一些随机数据,这些数据将放入一个数组中,稍后将使用。

On the date page of chance.js you can read that moment.js is also compatible with chance.js 机会.js日期页面上,您可以阅读一下moment.js也与机会.js兼容

I imported both libraires with NPM to my .html file in the following way: 我通过以下方式将两个带有NPM的库文件导入到我的.html文件中:

Node Server: 节点服务器:

app.use('/moment', express.static(__dirname + '/node_modules/moment/')); app.use('/chance', express.static(__dirname + '/node_modules/chance/'));

index.html index.html

<script type="text/javascript" src="../../moment/moment.js"></script>
<script type="text/javascript" src="../../chance/chance.js"></script>
<script type="text/javascript" src="testresults.js"></script>

Now i would like to create some random dates with a custom format. 现在,我想用自定义格式创建一些随机日期。

const testDateTime = chance.date({{moment().format('MMMM Do YYYY, h:mm:ss a')}});

This is not working because my IDE will tell me that moment has not been imported and cannot make use of the format. 这是行不通的,因为我的IDE会告诉我该时刻尚未导入,因此无法使用该格式。

What is the right way to use moment.js in combination with chance.js? 什么是将moment.js与偶然机会js结合使用的正确方法?

import anything in node like this 像这样导入节点中的任何内容

const Chance = require('chance');

Using chance 利用机会

var chance = new Chance();
chance.date()

Why would you use chance and moment in a combination when moment alone will do the same thing? 当单独的时刻会做同样的事情时,为什么要结合使用机会和时刻?

You do not need to add anything related to import in the script tag in html file other that your app.js if this may be your case. 如果可能的话,除了app.js之外,您无需在html文件的script标记中添加任何与import相关的内容。

In the chancejs website it is written 在偶然的网站上写

If you want richer control over date format, strongly suggest using the Moment library. 如果要对日期格式进行更丰富的控制,强烈建议使用Moment库。 Our formatting is very minimalist, and it's out of our core competency to offer dates in a myriad of formats. 我们的格式化非常简单,而且我们的核心能力是提供多种格式的日期。

This means you have to use moment for complex task in dates 这意味着您必须将时间用于日期中的复杂任务

moment website 瞬间网站

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM