简体   繁体   English

如何在Moment.js中区分一个月和一个月的第一天

[英]How to differentiate between a month, and the first day of a month in Moment.js

In moment I am parsing dates from CSV, with the option to add formatting. 目前,我正在解析CSV中的日期,并可以选择添加格式。

Sometimes the dates are dates and will be handled as points in time. 有时,日期是日期,将作为时间点处理。 Sometimes the dates are fragments, describing a year, a month and so on. 有时日期是片段,描述了一年,一个月等等。 This will be handled as a range. 这将作为范围进行处理。

See the following code: 请参见以下代码:

var a = moment('Jun 2014');
var b = moment('01-06-2014');

//output
a.toDate(); // Sun Jun 01 2014 00:00:00 GMT+0200 (CEST)
b.toDate(); // Sun Jun 01 2014 00:00:00 GMT+0200 (CEST)

I understand that a missing day will be interpreted as day 0 (or 1) but is there any way for me to differentiate between the date 1st June and the month June when parsing dates? 我知道缺少的一天将被解释为第0天(或第1天),但是我有什么办法可以区分解析日期的6月1日和6月?

Maybe it would be easiest to wrap your date parsing in another function that attaches metadata to the result. 也许将日期解析包装在另一个将元数据附加到结果的函数中是最简单的。 Here's a rough idea: 这是一个大概的想法:

function parseDate(d) {
  var ret = moment(d);

  if(d.indexOf('-') === -1) {
    ret.monthOnly = true;
  }

  return ret;
}

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

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