I created a function that produces a loop of days in month.
Here's my code
var date = new Date()
var firstDay = new Date(date.getFullYear(), date.getMonth(), 2);
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 1);
firstDay.setHours(0, 0, 0, 0)
lastDay.setHours(0, 0, 0, 0)
console.log('Today date: ', date)
console.log('First Day: ', firstDay)
console.log('Last Day: ', lastDay)
Why the First Day and Last Day starts at 17:00 ?
you can 'force' the dates to start at zero dark hundred by doing this:
const date = new Date()
date.setHours(0,0,0,0);
I suspect that your requirements might be more complex. If that is the case, you can force the user's browser/device to adopt a specific timezone, such as this:
date.toLocaleString('en-GB', { timeZone: 'UTC' });
And since you mentioned you are running it on Node.js which has a different timezone configuration,
const today = new Date(new Date().setUTCHours(0,0,0,0));
const todayISO = today.toISOString();
console.log(todayISO);
new Date()
This returns an object which represent the current time of the system where the command runs.
Thu Feb 28 2019 15:14:30 GMT+0530 (India Standard Time)
Related
Can any one help me to find first date and last date from month and year.
I have 2 parameter month and year, for example month = 10 and year = 2021
I tried some code but it doesn't have the answer
var prevMonthLastDay = new Date(2021, 10, 0);
var thisMonthFirstDay = new Date(2021, 10, 1);
console.log(prevMonthLastDay);
console.log(thisMonthFirstDay);
Above code is giving following output:
2021-10-30T17:00:00.000Z
2021-10-31T17:00:00.000Z
My expected answer is first date 2021-10-01 and last date 2021-10-31
Try the following to get your expected output:
function GetFirstAndLastDate(year, month)
{
var firstDayOfMonth = new Date(year, month-1, 2);
var lastDayOfMonth = new Date(year, month, 1);
console.log('First Day: ' + firstDayOfMonth.toISOString().substring(0, 10));
console.log('Last Day: ' + lastDayOfMonth.toISOString().substring(0, 10));
}
GetFirstAndLastDate(2021, 10);
You can try this way
var date = new Date();
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
I'm a little bit confused with what you really want to get referencing your variable names, so I include them all:
const currentYear = 2021;
const currentMonth = 10;
const prevMonthFirstDay = new Date(currentYear, currentMonth - 2, 1);
const prevMonthLastDay = new Date(currentYear, currentMonth - 1, 0);
const currentMonthFirstDay = new Date(currentYear, currentMonth - 1, 1);
const currentMonthLastDay = new Date(currentYear, currentMonth, 0);
const nextMonthFirstDay = new Date(currentYear, currentMonth, 1);
const nextMonthLastDay = new Date(currentYear, currentMonth + 1, 0);
console.log('prev month first day:', prevMonthFirstDay);
console.log('prev month last day:', prevMonthLastDay);
console.log('current month first day:', currentMonthFirstDay);
console.log('current month last day:', currentMonthLastDay);
console.log('next month first day:', nextMonthFirstDay);
console.log('next month last day:', nextMonthLastDay);
on my system this will gives:
prev month first day: Wed Sep 01 2021 00:00:00 GMT+0700 (Western Indonesia Time)
prev month last day: Thu Sep 30 2021 00:00:00 GMT+0700 (Western Indonesia Time)
current month first day: Fri Oct 01 2021 00:00:00 GMT+0700 (Western Indonesia Time)
current month last day: Sun Oct 31 2021 00:00:00 GMT+0700 (Western Indonesia Time)
next month first day: Mon Nov 01 2021 00:00:00 GMT+0700 (Western Indonesia Time)
next month last day: Tue Nov 30 2021 00:00:00 GMT+0700 (Western Indonesia Time)
so now it will only a matter of formatting:
console.log(currentMonthFirstDay.toLocaleDateString('en-GB').split('/').reverse().join('-'));
which will give you:
2021-10-01
You can follow this.
//first and last of the current month
var current_month = "April 2017";
var arrMonth = current_month.split(" ");
var first_day = new Date(arrMonth[0] + " 1 " + arrMonth[1]);
//even though I already have the values, I'm using date functions to get year and month
//because month is zero-based
var last_day = new Date(first_day.getFullYear(), first_day.getMonth() + 1, 0, 23, 59, 59);
//use moment,js to format
var start = moment(first_day).format("YYYY-MM-DD");
var end = moment(last_day).format("YYYY-MM-DD");
the answer is in the following link:
How to get last day of the month
You can try this way referrence
var date = new Date(), y = date.getFullYear(), m = date.getMonth();
var firstDay = new Date(y, m, 1);
var lastDay = new Date(y, m + 1, 0);
I have table of records and need to get only current month records.
CODE:
let startDate = req.body.startDate
let endDate = req.body.endDate
let result = await caseRegistration.findByDate({ pathology_id : req.body.pathology_id,
created_at: {
'>=': new Date(startDate),
'<=': new Date(endDate)
}
})
Above code I am passing particular dates to get records. But my requirement is If request doesn't have any date then I want to get only current month data. Can you please help me?
var date = new Date();
var firstDay = new Date(date.getFullYear(),date.getMonth(), 1);
var lastDay = new Date(date.getFullYear(),date.getMonth(), daysInMonth(date.getMonth()+1,
date.getFullYear()));
firstDay=>Tue Sep 01 2020 00:00:00 GMT+0530 (India Standard Time)
lastDay=> Wed Sep 30 2020 00:00:00 GMT+0530 (India Standard Time)
Please take care of timezone & date formate you want(it could be any)
If startDate and endDate are empty find first date and last date of current month by using this :
var date = new Date();
var firstDateOfCurrentMonth = new Date(date.getFullYear(), date.getMonth(), 1);
var endDateOfCurrentMonth = new Date(date.getFullYear(), date.getMonth() + 1, 0);
Now you can assign these dates to startDate and endDate
statDate=firstDateOfCurrentMonth;
endDate=endDateOfCurrentMonth;
I need to fetch the first and last day of the current month. I created the solution using the core JS new Date() method.
const date = new Date();
const firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
const lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
console.log(firstDay.toUTCString(), lastDay.toUTCString()); //Tue, 31 Mar 2020 18:30:00 GMT Wed, 29 Apr 2020 18:30:00 GMT
When I try a similar thing in the browser's console, it prints the result as expected, i.e. 1st April 2020 and 30th April 2020, but testing it in the postman environment, gives the wrong result.
Can anyone please help to resolve this confusion?
Your code:
var d = new Date(),
e = new Date(d.getFullYear(), d.getMonth(), 1),
f = new Date(d.getFullYear(), d.getMonth() + 1, 0);
console.log(e.toUTCString() + "\n" + f.toUTCString());
so you get wrong result because you want to get the UTC string
from local date, you need to make it UTC date in order to print
UTC string correctly.
var d = new Date(),
e = new Date(Date.UTC(d.getFullYear(), d.getMonth(), 1)),
f = new Date(Date.UTC(d.getFullYear(), d.getMonth() + 1, 0));
console.log(e.toUTCString() + "\n" + f.toUTCString());
I have referred to the link javascript getTime() to 10 digits only. And I got to know the way to convert date to 10 digit timestamp. My requirement is that I want last month date timestamp. Below is the code that I have tried:
var date = new Date();
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
var monthLastDay = Math.floor(lastDay.getTime()/1000);
console.log(monthLastDay);
When I run above code I get output: 1527705000. And when I check the output on https://www.unixtimestamp.com/index.php. I got
1527705000
Is equivalent to:
05/30/2018 # 6:30pm (UTC)
Which I think is not correct as the month 05 has 31 days. So I should timestamp as 05/31/2018.
EDIT:
If I try with below code:
var date = new Date('6/8/2018');
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
var monthLastDay = Math.floor(lastDay.getTime()/1000);
console.log(monthLastDay);
I get output: 1530297000 which is equal to 6/29/2018. But output should be 6/30/2018.
Let me know possible solution to get the correct 10 digit timestamp.
I guess the problem is with your timezone.
> new Date('6/8/2018')
Fri Jun 08 2018 00:00:00 GMT+0200 (CEST)
> new Date('6/8/2018').getTime()
1528408800000 // which is 06/07/2018 # 10:00pm (UTC)
You should build the date in UTC if you want to use the timestamp as is
> var date = new Date('6/8/2018');
> Date.UTC(date.getFullYear(), date.getMonth() + 1, 0);
1530316800000 // which is 06/30/2018 # 12:00am (UTC)
I am in GMT +2 timezone and Daylight saving time on.
My requirement is to ask user a date and convert it into UTC without DST consideration.
when I do console.log(new Date()),
it gives me "Wed Oct 26 2016 18:00:00 GMT+0300 (Turkey Daylight Time)"
I want to send server a UTC format date which server is going to save in Database.
var extractTime = new Date(timeof.edtime); //timeof.edtime contains date given by user.
var d = extractTime.getDate();
var m = extractTime.getMonth();
var y = extractTime.getFullYear();
var h = extractTime.getHours();
var mm = extractTime.getMinutes();
timeof.edtime = moment(new Date(y, m, d, h, mm)).utc().format("YYYY-MM-DD HH:mm");
After converting to utc, timeof.edtime is "2016-09-26 15:00" because it subtract 3 hours to go to GMT. (subtracted 2 hours of standard turkish time) (subtracted -1 of DST)
I want to subtract date to UTC by not considering DST and in our case my expectation is to get hour as "16:00" and not "15:00"
How to convert date to UTC without Daylight saving consideration.
any solution using moment js or jquery will be helpful.
I am testing on chrome.
by browsing through few link it says new Date() will give in standard time and doesn't consider DST consideration but this is not I observed and created plunk to reproduce new Date().toUTCString() consider DST as well, how to avoid subtraction of DST?
https://plnkr.co/edit/tjCOoJqXMHGzCD8B5LdL?p=preview
Inspired by this answer, you could make the time correction as follows:
function compensateDST(dt) {
var janOffset = new Date(dt.getFullYear(), 0, 1).getTimezoneOffset();
var julOffset = new Date(dt.getFullYear(), 6, 1).getTimezoneOffset();
var dstMinutes = dt.getTimezoneOffset() - Math.max(janOffset, julOffset);
dt = new Date(dt);
dt.setMinutes(dt.getMinutes() - dstMinutes);
return dt;
}
// Use your date here:
var extractTime = new Date('2016-10-26 18:00');
// Truncate to minute precision:
var extractTime = new Date(extractTime.getTime() - extractTime.getTime() % 60000)
console.log('Local time:', extractTime.toString());
console.log('UTC time before correction:', extractTime.toISOString());
// Compensate for the DST shift:
var extractTime = compensateDST(extractTime);
console.log('UTC time after correction:', extractTime.toISOString());
try this:
+180 is GMT+0300 (Turkey)
var x = new Date();
var newdate = new Date();
if((x.getTimezoneOffset()) != 180){
newdate = new Date(x.getTime() + (60000*(x.getTimezoneOffset()+180)));
}
console.log(newdate);