最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

date - Javascript remove time from datetime - Stack Overflow

programmeradmin4浏览0评论

I am using full calendar and using the date from the eventclick

calEvent._start._i         returns: 2015-12-01 09:00:00

and I want to pare it to another date although it is this format

2015-12-01

I have used

var date = new Date(calendarDate);
date.setHours(0,0,0,0);

which returns an invalid date format

I have also used

var calDate = _longDateFormat(calendarDate, "dd-mm-yy");

which errored on the javascript

Nothing seems to work, including dateFormat.

Ideas?

I am using full calendar and using the date from the eventclick

calEvent._start._i         returns: 2015-12-01 09:00:00

and I want to pare it to another date although it is this format

2015-12-01

I have used

var date = new Date(calendarDate);
date.setHours(0,0,0,0);

which returns an invalid date format

I have also used

var calDate = _longDateFormat(calendarDate, "dd-mm-yy");

which errored on the javascript

Nothing seems to work, including dateFormat.

Ideas?

Share edited Aug 26, 2019 at 2:07 Dale K 27.5k15 gold badges58 silver badges83 bronze badges asked Dec 1, 2015 at 10:57 bananabreadbobbananabreadbob 4073 gold badges13 silver badges30 bronze badges 4
  • what do you want to pare? Two Dates ignoring the time? – Martin Schneider Commented Dec 1, 2015 at 11:05
  • Yes, at the moment becausae the times are different it doesn't work – bananabreadbob Commented Dec 1, 2015 at 11:10
  • 1 Try this stackoverflow./a/19054782/5089795 – Martin Schneider Commented Dec 1, 2015 at 11:15
  • if (requestedDate.sameDay(calendarDate)){ return true; } when running through firebug it doesn't do anything – bananabreadbob Commented Dec 1, 2015 at 11:35
Add a ment  | 

2 Answers 2

Reset to default 3

I was facing the same problem and I didn't want to use specific formatting options because I wanted the date to be displayed in the correct local format. I ended up using a simple regex replace, maybe this approach will help you as well:

new Date().toLocaleString().replace(/\s\d{2}:\d{2}:\d{2,4}$/, ''))

You could do something like this:

function checkWhetherCorrectDate(requestedDate, calendarDate) {
    var date1 = new Date(calendarDate.replace(/-/g,'/'));
    var date2 = new Date(requestedDate);
    date1.setHours(date2.getHours(), date2.getMinutes(), date2.getSeconds());
    return date1.getTime() === date2.getTime();
}


document.write(checkWhetherCorrectDate("2015-12-01", "2015-11-30 09:00:00") + '<br />')
document.write(checkWhetherCorrectDate("2015-12-01", "2015-12-01 09:00:00") + '<br />')

发布评论

评论列表(0)

  1. 暂无评论