4

I'm trying to get the start date of a day and the end date. Now to do this I've written this code:

var date_start_temp = $('#calendar').fullCalendar('getView').start;
console.log(date_start_temp)
var date_start = date_start_temp.clone().utc().format("ddd MMM DD YYYY HH:mm:ss");
var date_end = date_start.clone().startOf('day').add(1, 'day').format("ddd MMM DD YYYY HH:mm:ss");

The console.log returns this:

Tue Nov 10 2015 01:00:00 GMT+0100 (ora solare Europa occidentale)

but in the next line I get this error:

date_start_temp.clone(...).utc is not a function

and I don't know why. I just want to get this final result:

date_start

Tue Nov 10 2015 00:00:00

date_end

Wed Nov 11 2015 00:00:00

How you can see I've set the hours to 0 and remove the GMT, I don't want the GMT. How I can achieve this??

AtheistP3ace
  • 9,611
  • 12
  • 43
  • 43
Dillinger
  • 1,823
  • 4
  • 33
  • 78
  • Possible duplicate of [How to clone a Date object in JavaScript](http://stackoverflow.com/questions/1090815/how-to-clone-a-date-object-in-javascript) – NendoTaka Nov 10 '15 at 19:40
  • I'm using momentjs no date object of javascript, read the post first. – Dillinger Nov 10 '15 at 19:42
  • Are you sure `.start` returns a momentjs object? Doesn't look like it, otherwise you wouldn't get an error. – Felix Kling Nov 10 '15 at 19:44
  • Yeah it does. I've printed the result in chrome console – Dillinger Nov 10 '15 at 19:45
  • That just tells you that the value represents a date. I get a similar output if I do `console.log(new Date())`. So I would say, pass the value to momentjs *before* you are trying to clone it. – Felix Kling Nov 10 '15 at 19:46

1 Answers1

8

but in the next line I get this error [...] and I don't know why.

.clone is a method of Moment.js. .start doesn't seem to return a Moment.js instance, so you cannot call .clone on it.

Pass the value to moment first:

var date_start = moment(date_start_temp).utc().format("ddd MMM DD YYYY HH:mm:ss");
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • Okay and for date_end? How you can see I've: var date_end = date_start.clone().startOf('day').add(1, 'day').format("ddd MMM DD YYYY HH:mm:ss"); – Dillinger Nov 10 '15 at 19:54
  • Pretty much the same: `moment(date_start_temp).utc().startOf('day').add(1, 'day').format("ddd MMM DD YYYY HH:mm:ss");` – Felix Kling Nov 10 '15 at 19:57
  • the date returned at the end is: Tue Nov 10 2015 00:00:00 GMT+0100, I want remove +0100 'cause add an hour in my timezone how I can do that? – Dillinger Nov 10 '15 at 20:03
  • Not sure what you mean. The way you format the data it won't show the timezone. Of course the underlying date object still contains timezone information. – Felix Kling Nov 10 '15 at 20:08