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;
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);
startDate = "2019-03-07 (목) 12:00";
var year = startDate.slice(0,4);
var month = startDate.slice(5,7);
var day = startDate.slice(8,10);
var hour = startDate.slice(15,17);
var minute = startDate.slice(18,20);
var selEndDatetime = new Date(year, month, day, hour, minute);
console.log(selEndDatetime);
I want to see "Tue Mar 07 2019 12:00:00 GMT+0900 (한국 표준시)", but console shows me the message "Sun Apr 07 2019 12:00:00 GMT+0900 (한국 표준시)".
What is wrong this code and how can I modify it to get the desired output?
The month starts at 0. You need to parse month from string to integer and minus 1 when creating a date instance.
startDate = "2019-03-07 (목) 12:00";
var year = startDate.slice(0,4);
var month = parseInt(startDate.slice(5,7))
var day = startDate.slice(8,10);
var hour = startDate.slice(15,17);
var minute = startDate.slice(18,20);
var selEndDatetime = new Date(year, month - 1, day, hour, minute);
console.log(selEndDatetime);
You can simply pass the startDate string as an argument to the Date in order to create selEndDatetime
Code:
const startDate = '2019-03-07 (목) 12:00';
const selEndDatetime = new Date(startDate);
console.log(selEndDatetime);
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 need date and time in this format in javascript.
2016/02/17-14:31:36
Currently I am using
var data = new Date();
But this gives Date {Fri Feb 26 2016 18:38:45 GMT+0530 (India Standard Time)}
var d = new Date(),
year = d.getFullYear(),
month = ('0'+(d.getMonth()+1)).slice(-2),
day = ('0'+d.getDay()).slice(-2),
hour = ('0'+d.getHours()).slice(-2),
minute = ('0'+d.getMinutes()).slice(-2),
second = ('0'+d.getSeconds()).slice(-2);
var dateString = year+'/'+month+'/'+day+'-'+hour+':'+minute+':'+second;
Next time, please google yourself. Thanks.
Edit
Now it also has those leading zero's.
I am trying to format a date so I can use a compare function to sort the data
$(xml).find("item").each(function () {
var dateText = $(this).find("Date").text();
var year = dateText.substr(0,4);
var month = dateText.substr(4,2) ;
var day = dateText.substring(6,2);
var newDate = new Date(year, month, day);
When I display the newDate I get this: Mon Jul 03 2017 00:00:00 GMT-0700 (Pacific Daylight Time) which isn't close to the actual dates which are in 2013 and 2014. Does anyone have a suggestion as to formating this correctly? Thanks!
Something like this should work
var dateText = $(this).find("Date").text();
if( dateText && dateText.length===8){
var year = dateText.substr(0,4);
var month = dateText.substr(4,2) ;
var day = dateText.substring(6,2);
var newDate = new Date(year, month, day);
}else{
dateText='TBD';
newDate='TBD';
}