- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
本质上,我正在重新创建 FCC 的后端项目之一:https://timestamp-ms.herokuapp.com/
如果我输入以下内容,它看起来就像我当前的代码: http://localhost:3000/1
它返回如下:
{"unix":"1","natural":"December 31, 1969"}
var express = require('express');
var path = require('path')
var app = express();
var moment = require('moment')
var port = 3000;
//homepage
app.get('/', function(req, res) {
var fileName = path.join(__dirname, 'index.html');
res.sendFile(fileName, function (err) {
if (err) {console.error(err)}
console.log('This is the homepage')
});
});
//input of the page
app.get('/:dataString', function(req, res) {
var dataString = req.params.dataString;
var output;
//Using regex, checks if the dataString has only number characters
if(/^[0-9]*$/.test(dataString)){
output = moment(dataString, "X")
} else{
output = moment(dataString, "MMMM D, YYYY")
}
if (output.isValid()){
res.json({
unix: output.format("X"),
natural: output.format("MMMM D, YYYY")
});
} else{
res.json({
unix: 'null',
natural: 'null'
});
}
})
app.listen(port,function(){
console.log("turn on. Port is: ", port)
})
我预计会返回 1970 年 1 月 1 日,但我不确定错误来自何处。
最佳答案
格林尼治标准时间 (GMT) 时区为“1970 年 1 月 1 日”。 Moment 很可能会按照您自己的时区来格式化日期,如果您位于 GMT 以西,则该日期将变为“1969 年 12 月 31 日”。
要格式化 UTC 时区的日期,您可以编写:
moment('1', 'X').utc().format('MMMM D, YYYY')
这将输出“1970 年 1 月 1 日”。
关于javascript - Moment.js 返回 "December 31, 1969",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38648380/
System.Math.MomentSkewKurtosis 声明为 procedure MomentSkewKurtosis(const Data: array of Double; var M1,
我尝试在应用程序中使用 moment.js 和 moment-timezone-with-data.js 来显示任何时区的日期。 我已将这两个 js 文件的 CDN 包含在我的 index.html
我对 Moment 函数的工作原理有点困惑(例如 startOf()、weekday() 等)。 我的问题是:如何从预定义的日期获取一周的开始? 考虑 9/21/16 上的这两个调用: moment(
阅读react-datepicker doc's后我尝试按照文档的说明更改日期选择器语言: Globally by calling moment.locale(lang) Picker-specifi
我有一个服务器生成的位置列表,采用国家/城市格式,即 Beijing London 我想使用 JS/Jquery 和 Moment Timezone 来执行此操作: 从每个项目中提取 data-tim
我有一个时刻对象和一个单独的时间字符串,如下所示: var selectedDay = moment(); var time = '6 PM'; 我想将两者结合起来,我无法从文档中弄清楚,就像这样。
我有一个时刻对象,我正在尝试添加几天。但是,它返回的结果是相同的 moment 对象。 但是,如果我只是尝试在当前日期执行此操作,则效果很好。 另外,请注意,我总是收到一个需要添加天数的时刻对象。 代
我有一个名为 date 的时刻对象: ( Tue Apr 09 2019 00:00:00 ...) 我想在最后一刻将其替换为同一天,如下所示: (Tue Apr 09 2019 23:59:59 .
目前我有一个输入字段,允许您通过输入两位数字来选择一个小时。例如,您可以选择 12 表示 12 小时。 有没有办法使用 MomentJS 将该数字转换为毫秒? 目前我必须做以下数学运算。我在 Mome
moment().format() 根据 moment().isValid() 创建无效日期 示例如下: > moment.version "2.14.1" > moment.locale() "fr
我正在使用 moment.js,但在测试 moment.js 库时,我继续收到错误。 var back30Days=moment().subtract(30, 'days').format("dddd
在使用 moment 构建 angular 8 项目时,我遇到了以下错误消息: ng build @myproject/common Error: Cannot call a namespace ('
我安装了 moment.js 库并准备稍微玩弄它,当我收到此错误时。我尝试了很多不同的东西,但没有任何效果。有任何想法吗? 我有一个文件夹,我已经将 moment.js 安装到(npm install
我有一个时间戳的初始值(在 User-1 的时区中创建),我正在从 firebase 的 firestore 中检索。其格式为 Unix 纪元的 UTC 秒数。 我的目标是获取初始时间戳值,并将其转换
有一个从元素的 innerText 设置的 startTime。然后使用 moment(startTime) 将该字符串转换为 Moment.js 对象。然后如何将其转换为以分钟为单位的数字值? Ja
我使用 moment.js 创建了一个 js 脚本,我想通过 cron 作业复制它。因此,它将使用 PHP。 所以,我搜索了 moment.js 的等价物,我找到了这个 https://github.
我得到一个文本值,它的格式如下:“9/19/2018 10:00 AM”。您将如何使用 moment.js 或仅使用 javascript 将其转换为纪元时间戳? 示例: console.log(mo
$> moment.version '2.18.1' $> moment('Mon, 02 Jan 2017 06:00:00 -0800').format() '2017-01-02T01:00:0
我有一个jsfiddle here 。 它是空白的,因为我想做的事情不起作用...... 我已经调用了适当的外部脚本。 我正在尝试将这个令人难以置信的库实现到我的网站中,但我不确定我做错了什么。 最终
我有 moment 对象,我想将其转换为格式为 dd.mm.yy 的字符串。使用 moment.js 执行此操作的最佳方法是什么? 我尝试了toString函数,但它返回的日期格式为Wed Mar 2
我是一名优秀的程序员,十分优秀!