【问题标题】:Get last day of the month from '2015-02-23' string (javascript) [duplicate]从'2015-02-23'字符串(javascript)获取本月的最后一天[重复]
【发布时间】:2015-02-23 17:15:15
【问题描述】:

我有一个表示日期的字符串,格式如下:2015-02-23

我需要使用此日期来获取该月的最后一天。 我应该如何进行必要的转换以实现这一目标?

【问题讨论】:

标签: javascript date date-conversion


【解决方案1】:

这是一个适合你的函数:

function getLastDayInMonth(s) {
  var date = new Date(s);
  var lastDate = date;
  var month = date.getMonth();
  
  while (date.getMonth() == month) {
    lastDate = date;
    date = new Date(lastDate.getTime() + 1000 * 60 * 60 * 24); //add 1 day
  }
  return lastDate.toDateString();
}

var lastDay = getLastDayInMonth('2015-02-23');
alert(lastDay);

【讨论】:

  • 谢谢!这就像一个魅力!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-13
  • 2016-06-12
  • 2013-11-25
  • 1970-01-01
  • 2013-04-27
  • 2017-09-06
相关资源
最近更新 更多