Shamoon
Shamoon

Reputation: 43491

How can I combine the date from one variable with the times of another with MomentJS?

I have three variables:

date = Tue Aug 02 2016 20:00:00 GMT-0400 (EDT);

startTime = Thu Jan 01 1970 09:00:00 GMT-0500 (EST)

endTime = Thu Jan 01 1970 10:00:00 GMT-0500 (EST)

I want to modify the startTime and endTime to keep their times, but use the date part from date. How can I accomplish this with momentjs?

Upvotes: 3

Views: 130

Answers (3)

nozzleman
nozzleman

Reputation: 9649

Even though JohnnyHK's answer is correct... since you asked specifically how to do it using moment.js:

var date = moment(new Date('Tue Aug 15 2016 20:00:00 GMT-0400 (EDT)'));
var startTime = moment(new Date('Thu Jan 01 1970 09:00:00 GMT-0500 (EST)'));
var endTime = moment(new Date('Thu Jan 01 1970 10:00:00 GMT-0500 (EST)'));

console.log("date: " + date.format());
console.log("startTime old: " + startTime.format());
console.log("endTime old: " + endTime.format());

startTime.year(date.year());
startTime.dayOfYear(date.dayOfYear());

endTime.year(date.year());
endTime.dayOfYear(date.dayOfYear());

console.log("startTime new: " + startTime.format());
console.log("endTime new: " + endTime.format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>

Upvotes: 1

VincenzoC
VincenzoC

Reputation: 31482

As I said in the comment, you can use moment setter and getters for year, month and day or use native js counterparts (getFullYear, getMonth, getDate).

Here a working example showing both solutions:

// Simulating Date object input
var date = moment('2016-08-02T20:00:00').toDate();
var startTime = moment('1970-01-01T09:00:00').toDate();
var endTime = moment('1970-01-01T10:00:00').toDate();

// Using momentjs
var mDate = moment(date);
var mStartTime =  moment(startTime);
mStartTime.year(mDate.year()).month(mDate.month()).day(mDate.day());
console.log(mStartTime.format());

// Pure JavaScript
endTime.setFullYear(date.getFullYear());
endTime.setMonth(date.getMonth());
endTime.setDate(date.getDate());
console.log(endTime.toISOString());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.0/moment.min.js"></script>

Note that as stated in the docs:

By default, moment parses and displays in local time.

If you need to work with timestamps you can use moment-timezone.

Upvotes: 1

JohnnyHK
JohnnyHK

Reputation: 311855

You can do this by using the get & set methods of moment:

var date = moment(new Date('Tue Aug 02 2016 20:00:00 GMT-0400 (EDT)'));
var startTime = moment(new Date('Thu Jan 01 1970 09:00:00 GMT-0500 (EST)'));
var endTime = moment(new Date('Thu Jan 01 1970 10:00:00 GMT-0500 (EST)'));

var year = date.get('year');
var month = date.get('month');
var day = date.get('day');

startTime.set({ year: year, month: month, day: day });
endTime.set({ year: year, month: month, day: day });

console.log(date.format(), startTime.format(), endTime.format());

Output (CST time zone):

2016-08-02T19:00:00-05:00 2016-08-02T08:00:00-05:00 2016-08-02T09:00:00-05:00

Upvotes: 3

Related Questions