- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试从文本格式解析日期,以查看其中是否包含毫秒和秒。例如
let text = '2016-02-02 14:30:34.234';
const timezone = 'America/Los_Angeles';
// const hasMS = utcParse('%Y-%m-%d %H:%M:%S.')(text);
// const hasSeconds = utcParse('%Y-%m-%d %H:%M:')(text);
const hasMS = !!moment.tz(text, 'YYYY-MM-DD HH:MM:ss.', timezone);
console.log('hasMS', hasMS);
const hasSeconds = !!moment.tz(text, 'YYYY-MM-DD HH:MM:', timezone);
console.log('hasSeconds', hasSeconds);
基本上我正在尝试替换注释的代码。 d3.time-format 中的 utcParse() 将检查日期文本中是否包含毫秒和秒。我为 momentjs 库尝试了一些方法,但似乎不起作用。
最佳答案
通过使用正则表达式,您可以检查字符串的某些部分是否存在。
如果您使用字符串调用 matchDateTime()
,您将获得一个包含所有匹配组的数组。
let text_min = '2016-02-02 14:30';
let text_sec = '2016-02-02 14:30:34';
let text_ms = '2016-02-02 14:30:34.234';
function matchDateTime(text) { return text.match(/(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d)(?::(\d\d)(?:\.(\d\d\d))?)?/); }
function hasSeconds(text) { return !!matchDateTime(text)[6]; }
function hasMilliSeconds(text) { return !!matchDateTime(text)[7]; }
function testTimeString(text) {
console.log(text, "hasSeconds=", hasSeconds(text));
console.log(text, "hasMilliSeconds=", hasMilliSeconds(text));
}
testTimeString(text_min);
testTimeString(text_sec);
testTimeString(text_ms);
console.log(matchDateTime(text_ms));
关于javascript - 检查momentjs中日期文本是否有毫秒和秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51867938/
我正在尝试比较两个日期。我想检查一个日期是否早于另一个日期。我正在使用 momentjs,我所做的一切似乎都返回 false。任何帮助,将不胜感激。 Format - [Month, Year] mo
我正在尝试比较两个日期。我想检查一个日期是否早于另一个日期。我正在使用 momentjs,我所做的一切似乎都返回 false。任何帮助,将不胜感激。 Format - [Month, Year] mo
使用Moment.js将小时分钟转换为十进制时间的最佳方法是什么 假设我有1:30分钟,我想在转换后有1.5分钟 这是我尝试过的 console.log(travelTime().split(':')
我刚刚创建了一个 Ghost.org 博客,我希望博客发布日期比实际撰写日期提前一年显示。我知道 Ghost 使用 Moment.js 并且我能够调整格式(DD MM YYYY、YY MM DD 等.
我是 moment.js 的新手。我想将毫秒转换为天。当我在谷歌中尝试使用普通转换器时,它显示 377.7613437963 days ,但以编程方式显示 11 days . var duration
我有一个像“Thu Sep 01 2016 00:00:00 GMT+0530 (IST)”这样的日期,我需要将它作为 ISO-8601 UTC 时间发送到服务器。我试过: moment(mydate
我正在使用时刻时区来设置默认时间 $moment.tz.setDefault('欧洲/伦敦'); $moment().toString(); 正在为我提供定义时区的时间。但我需要时间戳。所以我正在使用
我有一个像"10/14/2014 5:30:00 AM"这样的字符串。我需要将其解析为UTC字符串。但是,当我执行moment.utc("10/14/2014 5:30:00 AM").format(
var startDate = moment("2017-06-30 00:00 +0000", "YYYY-MM-DD HH:mm Z"); var endDate = startDate.clo
我试图确定一个时刻对象是否使用时间值(除了日期)进行了实例化。例如: var date = moment("2014-01-16"); date.format("h:mm a"); // 12:00
我有一个简单的系统,它在节点创建表单上允许用户选择开始和结束日期以及小时和分钟: 问题是日期在中间的某个地方搞砸了,或者我不知道如何转换它。 在数据库中,两个值被保存如下: start: "2019-
我想用 moment.js 计算常见的日期范围,例如: * 昨天 * 星期 * 本周至今 * 四分之一 * 季度至今 等等。我没有在 Stackoverflow 上找到我想要的东西,所以我想我会为其他
我想使用 momentjs 来检查无效的日期/时间字符串: var invalid = '2017-03-18 23;00;00'; if (moment(invalid).isValid()) {
嗨,我目前在我的项目中使用 momentjs 作为我的日期,并且在减去 2 个日期时遇到了一些麻烦。 这是我的示例日期: 2016-10-08 10:29:23 2016-10-08 11:06:55
我正在使用moment js转换日期变量格式 moment("23MAY68", 'DDMMMYY').format('YYYY-MM-DD'); moment("23MAY99", 'DDMMMYY
我在使用 momentjs 库时遇到问题 线 moment("Mon Oct 14 01:00:00 GMT 2013") parses correctly 但线 moment("Mon Oct 1
我有一个函数可以获取格式为 YYYY-MM-DD 的日期键,例如。 2021-03-29(星期一)获取其周数 (13),并使用它获取该周数的星期一,如果是 2021-03-29,则为同一天,如果是 2
我需要获得从现在到今天的特定小时的秒数。 moment().diff(moment().hour(23), 'seconds'); 精度总是以小时为单位,我需要以秒为单位。 例如,如果现在是 15:2
我正在尝试使用时刻来开始新的一天。我得到以下不同的结果: moment().startOf('day'); //good moment(new Date()).startOf('day'); //th
我有一个简单的用例,但我无法在moment.js中弄清楚如何从GMT / UTC转换为本地时间。 例: var gmtDateTime = moment.utc("2015-10-24 20:00",
我是一名优秀的程序员,十分优秀!