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)
Related
This question already has answers here:
How to add days to Date?
(56 answers)
Closed 2 years ago.
I am trying to pull some historical data, which has a variety of time stamps. I want the use to a user selected date, and then pull all days + some additional days.
Date1 is
var Date1 = "Thu Oct 22 00:00:00 GMT-04:00 2020"
var startDate = new Date(Date1)
var Enddate = new Date();
Enddate.setDate(startDate + 10);
This doesn't work. I cant seem to figure out how to add the "10 days" to the Date1 variable.
You need the getDate function of the Date object, and you'll need to pass the correct value when instantiating the new Date.
Try this:
var Date1 = "Thu Oct 22 00:00:00 GMT-04:00 2020"
var startDate = new Date(Date1);
// add it to startDate, using getDate()
startDate.setDate(startDate.getDate() + 10);
// now startDate will be 10 days later:
startDate; // Sun Nov 01 00:00:00 GMT-04:00 2020
// if you want an entirely new Date object, then instantiate a new one:
var Enddate = new Date(startDate);
If you want two different variables, then you can use the approach similar to what you tried, like so:
var Date1 = "Thu Oct 22 00:00:00 GMT-04:00 2020"
var startDate = new Date(Date1), second = new Date(Date1); // we're using another temporary variable
// add the days
second.setDate(second.getDate() + 10);
// now use Enddate with this
var Enddate = new Date(second);
Enddate; // Sun Nov 01 00:00:00 GMT-04:00 2020
You can't just add a number to a Date object to get a new date.
You can add to a date by:
getting the current day of the month with getDate()
adding a number of days to that result
setting the new date with setDate()
const Date1 = "Thu Oct 22 00:00:00 GMT-04:00 2020";
const addDays = 10;
const startDate = new Date(Date1);
const startDays = startDate.getDate();
const newDate = new Date(startDate);
newDate.setDate(startDays + addDays);
console.log('startDate:', startDate.toISOString());
console.log('newDate:', newDate.toISOString());
Note:
Passing a string into the Date constructor is discouraged, as it produces inconsistent results across browsers.
It is better to pass in the individual date and time values:
const Date1 = new Date(2020, 9, 22, 0, 0, 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 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)
How can I obtain the last day of the month with the timestamp being 11:59:59 PM?
function LastDayOfMonth(Year, Month) {
return new Date((new Date(Year, Month, 1)) - 1);
}
console.log(LastDayOfMonth(2009, 11))
Example:
> LastDayOfMonth(2009, 11)
Mon Nov 30 2009 23:59:59 GMT+0100 (CET)
This will give you last day of current month.
var t= new Date();
alert(new Date(t.getFullYear(), t.getMonth() + 1, 0, 23, 59, 59));
var d = new Date();
console.log(d);
d.setMonth(d.getMonth() + 1); // set month as next month
console.log(d);
d.setDate(0); // get the last day of previous month
console.log(d);
Here is output from the code above:
Thu Oct 03 2013 11:34:59 GMT+0100 (GMT Daylight Time)
Sun Nov 03 2013 11:34:59 GMT+0000 (GMT Standard Time)
Thu Oct 31 2013 11:34:59 GMT+0000 (GMT Standard Time)
var d = new Date();
m = d.getMonth(); //current month
y = d.getFullYear(); //current year
alert(new Date(y,m,1)); //this is first day of current month
alert(new Date(y,m+1,0)); //this is last day of current month
var month = 1; // 1 for January
var d = new Date(2015, month, 0);
console.log(d); // last day in January
Sometimes all you have is a text version of the current month, ie: April 2017.
//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");
Last day of the month
now = new Date
lastDayOfTheMonth = new Date(1900+now.getYear(), now.getMonth()+1, 0)
Most of these answers are missing one thing or another. After playing with most of them I came up with the following that gives you the last possible millisecond of the month.
let testDate = new Date();
console.log(getMonthEnd(testDate));
function getMonthEnd(value) {
return new Date(value.getFullYear(), value.getMonth() + 1, 0, 23, 59, 59, 999);
}
Probably missing something in this one too but seems to cover all my requirements.
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DATE, cal.getActualMaximum(Calendar.DATE));
Date lastDayOfMonth = cal.getTime();
Do not forget month started with 0 so +1 in month too.
let enddayofmonth = new Date(year, month, 0).getDate();