κ°μ₯ κ°κΉμ΄ 15λΆ κ°κ²©μΌλ‘ λ°μ¬λ¦Ό λλ λ΄λ¦Όνλ λ°©λ²μ΄ μλμ§ κΆκΈν©λλ€. datejsμ κ°μ μΌλΆ λΌμ΄λΈλ¬λ¦¬μλ κ²μν λ λ³Έ κ²κ³Ό κ°μ κΈ°λ₯μ΄ μκΈ° λλ¬Έμ μ΄ κΈ°λ₯μ΄ κ³§ μμΌλ©΄ μ’μ κ² κ°μ΅λλ€. λ΄ μλ리μ€λ 15λΆ λ²ν·μΌλ‘ λΆν λ λ°μ΄ν° ν¬μΈνΈκ° μλ€λ κ²μ λλ€. μ ν λꡬλ μκ° λΈλ‘μ λ°ννκ³ κ°μ₯ κ°κΉμ΄ 15λΆμΌλ‘ λ°μ¬λ¦Όνκ³ μ’ λ£ μκ°μ λ°μ¬λ¦Όν΄μΌ ν©λλ€.
μ΄λ κ² νλ©΄ (http://stackoverflow.com/questions/4968250/how-to-round-time-to-the-nearest-quarter-hour-in-javascript) κ³ ν΅μ€λ¬μΈ κ² κ°μ΅λλ€. lib μ체μ μμΌλ©΄ μ μ©ν©λλ€.
moment.fn.roundMinutes = function () { return ~(this.minutes() / 15) * 15; }
λλ μ΄κ²μ μ¬μ©νκΈ° μν΄ μ΄κ²μ λ§λ€μκ³ λ€λ₯Έ μ¬λλ€μκ² λμμ΄ λ μ μλ€κ³ μκ°νμ΅λλ€. κ°μ₯ κ°κΉμ΄ NEXT 15λΆμΌλ‘ λ°μ¬λ¦Όλ©λλ€.
moment.fn.roundNext15Min = function () {
var intervals = Math.floor(this.minutes() / 15);
if(this.minutes() % 15 != 0)
intervals++;
if(intervals == 4) {
this.add('hours', 1);
intervals = 0;
}
this.minutes(intervals * 15);
this.seconds(0);
return this;
}
νΈμ§: this.add('hours', 1)λ₯Ό μν΄ this.incrementHours()λ₯Ό μ κ±°νμ΅λλ€.
μκ°μ μ΄ ν¨μλ₯Ό μΆκ°ν μ μλμ.. κ°μ₯ κ°κΉμ΄ λ€μ, μ΄μ μκ° κ°κ²©μΌλ‘ λ°μ¬λ¦Όν μ μλ€λ©΄ μ λ§ μ’μ κ²μ λλ€... μ΄μ λν μλ°±λ§ κ°μ Google κ²°κ³Όμ μλ°± κ°μ μ€ν μ€λ²νλ‘ μ°¬μ±νκ° μμ΅λλ€..
μ΄μ λ€μ 보λ incrementHoursκ° μ΄λ»κ² μλνλμ§ μ ν λͺ¨λ¦
λλ€.
μμ λ μ½λλ‘ λ¬Έμ μ λν κ²μλ¬Όμ μμ νμ΅λλ€. μ¦κΈ°λ€!
2014λ
3μ 10μΌ μμμΌ μ€ν 2μ 29λΆ, Blake Niemyjski
μλ¦Ό@github.comμμ± :
incrementHoursμ κΈ°λ₯μ 무μμ΄μμ΅λκΉ? λλ κ·Έκ²μ΄ λν μΌμ νλ€κ³ μΆμΈ‘νλ€
λ.μ΄ μ΄λ©μΌμ μ§μ λ΅μ₯νκ±°λ Gi tHubhttps://github.com/moment/moment/issues/959#issuecomment -37223821μμ νμΈνμΈμ.
.
@zbarnett λ°©λ²μ κ°μ¬λ립λλ€. λ΄ νλ‘μ νΈμ μ νν νμν κ²
startOf(15, 'm')
μ κ°μ startOf μΈν°νμ΄μ€λ₯Ό μ¬μ©ν μ μμ΅λλ€. λͺ¨λ μ λμμ μλν μ μμ΅λλ€. PRμ κΈ°λ€λ¦¬κ³ μμ΅λλ€ :)
λ€μμ λ§€μ° κ°λ¨ν 곡μμ λλ€(Coffeescript ꡬ문μμ, μ΄ λ¬Έμ κ° λ°μν 컨ν μ€νΈμ΄κΈ° λλ¬Έμ λλ€).
round_interval = 15
remainder = time.minute() % round_interval
time.subtract('minutes', remainder).add('minutes', if remainder > round_interval / 2 then round_interval else 0)
λλ μ§κΈ λ€λ₯Έ μΌλ‘ μΆ©λΆν λ°μμ§λ§ λκ΅°κ° μ΄ μ½λλ₯Ό κ°μ Έμμ PRμ λ°λ‘ λ£κ³ μΆλ€λ©΄ μμ λ‘κ² νμμμ€.
#1595 μ°¬μ±μΌλ‘ λ§κ°
볡μ‘νκ³ κ°λ¨ν μνμ΄ νμ μμ΅λλ€.
const rounded = Math.round(moment().minute() / 15) * 15;
const roundedDown = Math.floor(moment().minute() / 15) * 15;
const roundedUp = Math.ceil(moment().minute() / 15) * 15;
moment().minute(roundedUp).second(0)
κ°μ₯ κ°κΉμ΄ 15λΆ λΆκΈ°λ‘ μΆλ ₯ν©λλ€.
@janwerkhoven μ μ루μ μ΄ μλνμ§ μμΌλ―λ‘ λ€μκ³Ό κ°μ΅λλ€.
function round15(minute) {
let intervals = [15, 30, 45, 59, 0];
let closest;
let min = 90;
for (let i = 0; i < intervals.length; i++) {
let iv = intervals[i];
let maybeMin = Math.abs(minute - iv);
if (maybeMin < min) {
min = maybeMin;
closest = iv;
}
}
if (closest === 59) {
closest = 0;
}
return closest;
}
let myMoment = moment();
myMoment.minutes(round15(myMoment.minutes()));
@ground5hark
function nearestMinutes(interval, someMoment){
const roundedMinutes = Math.round(someMoment.clone().minute() / interval) * interval;
return someMoment.clone().minute(roundedMinutes).second(0);
}
function nearestPastMinutes(interval, someMoment){
const roundedMinutes = Math.floor(someMoment.minute() / interval) * interval;
return someMoment.clone().minute(roundedMinutes).second(0);
}
function nearestFutureMinutes(interval, someMoment){
const roundedMinutes = Math.ceil(someMoment.minute() / interval) * interval;
return someMoment.clone().minute(roundedMinutes).second(0);
}
const now = moment();
const nearest5min = nearestMinutes(5, now);
const nearest15min = nearestMinutes(15, now);
const nearest30min = nearestMinutes(30, now);
const nearestFuture5min = nearestFutureMinutes(5, now);
const nearestPast5min = nearestPastMinutes(5, now);
μ½λ νμμ ν μ€νΈλ¨
μ€μν κ²μ .clone()
μ
λλ€. κ·Έκ²μ΄ μμΌλ©΄ Moment.jsλ₯Ό μ¬μ©ν λ μΌλ°μ μΈ ν¨μ μΈ μλ moment()
λ₯Ό νΈμ§νκ² λ©λλ€.
μ΄ λ μλ€μ μλνμμμ€
time = moment("01:46", "HH:mm");
round_interval = 30;//15;
intervals = Math.floor(time.minutes() / round_interval);
minutesToRound = time.minutes() % round_interval;
minutesRounded = minutesToRound>round_interval/2 ? round_interval: 0;
minutes = intervals * round_interval + minutesRounded;
time.minutes(minutes);
alert(time.format("HH:mm"))
νν°μ λ¦μμ§λ§...
@janwerkhoven μ nearestMinutes
ν¨μκ° μ¬λ°λ₯΄μ§ μμ΅λλ€. μ΄λ λΆμ λ°λΌ λ°μ¬λ¦Ό λλ λ΄λ¦Όνλ€λ κ²μ μλ―Ένλ―λ‘ κ°μ₯ κ°κΉμ΄ 15λ‘ λ°μ¬λ¦Όνλ©΄ 12:34 -> 12:30
λ° 12:39 -> 12:45
μμλ©λλ€.
const roundedMinutes = Math.round(someMoment.clone().minute() / interval) * interval;
// 12:34 is fine...
// => Math.round(34 / 15) * 15
// => 2 * 15
30
// ... but 12:39 is not - it should be 45
// => Math.round(39 / 15) * 15
// => 2 * 15
30
@Silviusconcept μ μ루μ
μ minutesRounded
μ΄λ₯Ό 극볡ν©λλ€.
@kevlarr
Math.round(39/15)
λ 2κ° μλ 3μ λ°νν©λλ€.
Math.round(39/15) * 15
λ 30μ΄ μλ 45λ₯Ό λ°νν©λλ€.
γ
γ
γ
γ
νλ£¨κ° λλ λ μ μ μμμ μ°λ λλ₯Ό λ무 λ§μ΄ μ½μ΄μ μ»μ건λ°.. Math.floor
λλ _zbarnett μ루μ
μ μ’μν©λλ€
ν¨μμ λν μ½κ°μ μ
λ°μ΄νΈ _ this.milliseconds(0); _
var newdate=turno.date.clone().roundNext15Min().add(μμ, 'λΆ');
moment.fn.roundNext15Min = ν¨μ() {
var κ°κ²© = Math.floor(this.minutes() / 15);
if(this.minutes() % 15 != 0)
κ°κ²©++;
if(κ°κ²© == 4) {
this.add('μκ°', 1);
κ°κ²© = 0;
}
this.minutes(κ°κ²© * 15);
this.seconds(0);
_ this.milliseconds(0); _
μ΄κ²μ λ°ννμμμ€;
}
λ΄ λ²μ μ λ΅λ³
const startOf = (m, n, unit) => {
const units = [
'year',
'month',
'hour',
'minute',
'second',
'millisecond',
];
const pos = units.indexOf(unit);
if (pos === -1) {
throw new Error('Unsupported unit');
}
for (let i = pos + 1; i < units.length; i++) {
m.set(units[i], 0);
}
m.set(unit, Math.floor(m.get(unit) / n) * n);
return m;
};
const endOf = (m, n, unit) => {
const units = [
'year',
'month',
'hour',
'minute',
'second',
'millisecond',
];
const pos = units.indexOf(unit);
if (pos === -1) {
throw new Error('Unsupported unit');
}
for (let i = pos + 1; i < units.length; i++) {
m.set(units[i], units[i] === 'millisecond' ? 999 : 59);
}
m.set(unit, Math.floor(m.get(unit) / n) * n + n - 1);
return m;
};
λ€μκ³Ό κ°μ΄ μ¬μ©νμμμ€.
startOf(moment(), 15, 'minute');
endOf(moment(), 15, 'minute')
// with moment-timezone
startOf(moment().tz("Europe/London"), 15, 'minute');
μ΄ ν¨μλ μλ κ°μ²΄λ₯Ό λ³κ²½νκ³ νμν λ clone()
λ₯Ό μ¬μ©ν©λλ€.
λ€λ₯Έ μ루μ
μ λ€μκ³Ό κ°μ΅λλ€.
let next15Minutes = moment().add(15, 'minutes');
next15Minutes.minutes(Math.floor(next15Minutes.minutes() / 15) * 15);
next15Minutes.format('HH:mm');
κ°μ₯ μ μ©ν λκΈ
볡μ‘νκ³ κ°λ¨ν μνμ΄ νμ μμ΅λλ€.
κ°μ₯ κ°κΉμ΄ 15λΆ λΆκΈ°λ‘ μΆλ ₯ν©λλ€.