MomentJS - Subtrair
Assim como o método add, subtract permite subtrair dias, meses, horas, minutos, segundos, etc., de uma determinada data.
Sintaxe
moment().subtract(Number, String);
moment().subtract(Duration);
moment().subtract(Object);
Observe o seguinte exemplo que mostra como usar o método de subtração -
Exemplo
<html>
<head>
<title>MomentJS - Subtract Method</title>
<script type="text/JavaScript" src="https://MomentJS.com/downloads/moment.js"></script>
<style>
div {
border: solid 1px #ccc;
padding:10px;
font-family: "Segoe UI",Arial,sans-serif;
width: 75%;
}
</style>
</head>
<body>
<h1>MomentJS - Subtract Method</h1>
<div style="font-size:25px" id="currentdate"></div>
<br/>
<br/>
<div style="font-size:25px" id="changeddate"></div>
<br/>
<br/>
<div style="font-size:25px" id="changeddate1"></div>
<br/>
<br/>
<div style="font-size:25px" id="changeddate2"v</div>
<script type="text/JavaScript">
var day = moment();
document.getElementById("currentdate").innerHTML = "Current Date: " + day._d;
var changeddate = moment().subtract(5, 'days').subtract(2, 'months');
document.getElementById("changeddate").innerHTML = "Subtracting 5 days and 2 month using chaining method: " + changeddate._d;
var changeddate1 = moment().subtract({ days: 5, months: 2 });
document.getElementById("changeddate1").innerHTML = "Subtracting 5 days and 2 month using object method: " + changeddate1._d;
var duration = moment.duration({ 'days': 10 });
var changeddate2 = moment([2017, 10, 15]).subtract(duration);
document.getElementById("changeddate2").innerHTML = "Subtracting 10 days from given date using duration method: " + changeddate2._d;
</script>
</body>
</html>
Resultado
Para subtrair dias, meses da data que fizemos a seguir -
//chaining subtract method
var changeddate = moment().subtract(5, 'days').subtract(2, 'months');
// subtract object method
var changeddate1 = moment().subtract({ days: 5, months: 2 });
//using duration in subract method
var duration = moment.duration({ 'days': 10 });
var changeddate2 = moment([2017, 10, 15]).subtract(duration);
A saída para o mesmo é mostrada no exemplo acima.