type tutorial script principiantes para node llamar libro insertar externo etiqueta desde cero archivo javascript momentjs

javascript - tutorial - Momento JS inicio y fin del mes dado



node js tutorial pdf (11)

Cuando usas .endOf() estás mutando el objeto al que está llamado, entonces startDate convierte en Sep 30

Debería usar .clone() para hacer una copia en lugar de cambiarlo

var startDate = moment(year + ''-'' + month + ''-'' + 01 + '' 00:00:00''); var endDate = startDate.clone().endOf(''month''); console.log(startDate.toDate()); console.log(endDate.toDate()); Mon Sep 01 2014 00:00:00 GMT+0700 (ICT) Tue Sep 30 2014 23:59:59 GMT+0700 (ICT)

Necesito calcular una fecha JS dado año = 2014 y mes = 9 (septiembre de 2014).

Intenté esto:

var moment = require(''moment''); var startDate = moment( year+''-''+month+''-''+01 + '' 00:00:00'' ); var endDate = startDate.endOf(''month''); console.log(startDate.toDate()); console.log(endDate.toDate());

Ambos registros muestran:

Tue Sep 30 2014 23:59:59 GMT+0200 (CEST) Tue Sep 30 2014 23:59:59 GMT+0200 (CEST)

La fecha de finalización es correcta pero ... ¿por qué la fecha de inicio no lo es?


El siguiente código debería funcionar:

$(''#reportrange'').daterangepicker({ startDate: start, endDate: end, ranges: { ''Hoy'': [moment(), moment()], ''Ayer'': [moment().subtract(1, ''days''), moment().subtract(1, ''days'')], ''Ultimos 7 dias'': [moment().subtract(6, ''days''), moment()], ''Ultimos 30 dias'': [moment().subtract(29, ''days''), moment()], ''Mes actual'': [moment().startOf(''month''), moment().endOf(''month'')], ''Ultimo mes'': [moment().subtract(1, ''month'').startOf(''month''), moment().subtract(1, ''month'').endOf(''month'')], ''Enero'': [moment().month(0).startOf(''month'') , moment().month(0).endOf(''month'')], ''Febrero'': [moment().month(1).startOf(''month'') , moment().month(1).endOf(''month'')], ''Marzo'': [moment().month(2).startOf(''month'') , moment().month(2).endOf(''month'')], ''Abril'': [moment().month(3).startOf(''month'') , moment().month(3).endOf(''month'')], ''Mayo'': [moment().month(4).startOf(''month'') , moment().month(4).endOf(''month'')], ''Junio'': [moment().month(5).startOf(''month'') , moment().month(5).endOf(''month'')], ''Julio'': [moment().month(6).startOf(''month'') , moment().month(6).endOf(''month'')], ''Agosto'': [moment().month(7).startOf(''month'') , moment().month(7).endOf(''month'')], ''Septiembre'': [moment().month(8).startOf(''month'') , moment().month(8).endOf(''month'')], ''Octubre'': [moment().month(9).startOf(''month'') , moment().month(9).endOf(''month'')], ''Noviembre'': [moment().month(10).startOf(''month'') , moment().month(10).endOf(''month'')], ''Diciembre'': [moment().month(11).startOf(''month'') , moment().month(11).endOf(''month'')] } }, cb);


Eso es porque endOf muta el valor original.

Cita relevante:

Muta el momento original al establecerlo al final de una unidad de tiempo.

Aquí hay una función de ejemplo que le brinda la salida que desea:

function getMonthDateRange(year, month) { var moment = require(''moment''); // month in moment is 0 based, so 9 is actually october, subtract 1 to compensate // array is ''year'', ''month'', ''day'', etc var startDate = moment([year, month - 1]); // Clone the value before .endOf() var endDate = moment(startDate).endOf(''month''); // just for demonstration: console.log(startDate.toDate()); console.log(endDate.toDate()); // make sure to call toDate() for plain JavaScript date type return { start: startDate, end: endDate }; }

Referencias


Prueba el siguiente código:

const moment=require(''moment''); console.log("startDate=>",moment().startOf(''month'').format("YYYY-DD-MM")); console.log("endDate=>",moment().endOf(''month'').format("YYYY-DD-MM"));


Prueba el siguiente código:

moment(startDate).startOf(''months'') moment(startDate).endOf(''months'')


Realmente no creo que haya algún método directo para obtener el último día, pero podría hacer algo como esto:

var dateInst = new moment(); /** * adding 1 month from the present month and then subtracting 1 day, * So you would get the last day of this month */ dateInst.add(1, ''months'').date(1).subtract(1, ''days''); /* printing the last day of this month''s date */ console.log(dateInst.format(''YYYY MM DD''));


puede usar esto directamente para la fecha final o inicial del mes

new moment().startOf(''month'').format("YYYY-DD-MM"); new moment().endOf("month").format("YYYY-DD-MM");

puede cambiar el formato definiendo un nuevo formato


su fecha de inicio es el primer día del mes, en este caso podemos usar

var endDate = moment(startDate).add(1, ''months'').subtract(1, ''days'');

¡¡Espero que esto ayude!!


const year = 2014; const month = 09; // months start at index 0 in momentjs, so we subtract 1 const startDate = moment([year, month - 1, 01]).format("YYYY-MM-DD"); // get the number of days for this month const daysInMonth = moment(startDate).daysInMonth(); // we are adding the days in this month to the start date (minus the first day) const endDate = moment(startDate).add(daysInMonth - 1, ''days'').format("YYYY-MM-DD"); console.log(`start date: ${startDate}`); console.log(`end date: ${endDate}`);

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"> </script>


const dates = getDatesFromDateRange("2014-05-02", "2018-05-12", "YYYY/MM/DD", 1); console.log(dates); // you get the whole from-to date ranges as per parameters var onlyStartDates = dates.map(dateObj => dateObj["to"]); console.log(onlyStartDates); // moreover, if you want only from dates then you can grab by "map" function function getDatesFromDateRange( startDate, endDate, format, counter ) { startDate = moment(startDate, format); endDate = moment(endDate, format); let dates = []; let fromDate = startDate.clone(); let toDate = fromDate.clone().add(counter, "month").startOf("month").add(-1, "day"); do { dates.push({ "from": fromDate.format(format), "to": ( toDate < endDate ) ? toDate.format(format) : endDate.format(format) }); fromDate = moment(toDate, format).add(1, "day").clone(); toDate = fromDate.clone().add(counter, "month").startOf("month").add(-1, "day"); } while ( fromDate < endDate ); return dates; }

Tenga en cuenta que .clone () es esencial en el momento; de lo contrario, anulará el valor. Parece en tu caso.

Es más genérico, obtener un montón de fechas que caen entre fechas.


var d = new moment(); var startMonth = d.clone().startOf(''month''); var endMonth = d.clone().endOf(''month''); console.log(startMonth, endMonth);

doc