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';
}
Related
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;
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'm not that good at JS, so I have a problem with dates.
I want to set range for my DatePickers - that's why when the date is changed, I get it in the following format:
Wed Jul 08 2015 00:00:00 GMT+0200 (CEST)
I need to change it to the date in format dd/mm/yyyy to use the value later. Till now it looks like following:
var nowTemp = new Date();
$("#datepickerStart").on("changeDate", function (e) {
minDate = e.date;
console.log(minDate); // Wed Jul 08 2015 00:00:00 GMT+0200 (CEST)
// day = minDate.getDate(); <- doesn't work
// monthIndex = minDate.getMonth();
// year = minDate.getFullYear();
});
$('#datepickerStart').datepicker({
format: "dd/mm/yyyy",
startDate : new Date('01/01/2009'),
endDate : new Date()
});
$('#datepickerEnd').datepicker({
format: "dd/mm/yyyy",
startDate : minDate,//""+day+"/"+monthIndex+"/"+year,<- tried different approaches
endDate : new Date()
});
If I use minDate.format("dd/mm/yyyy"), it returns error "Uncaught TypeError: minDate.format is not a function".
Thanks in advance for help.
A good alternative for doing this is using momenjs library.
Other alternative is that you create a function which formats your date as follow:
var formatDate = function(d) {
var date = d.getDate();
var month = d.getMonth();
var year = d.getFullYear();
var newDate = (date < 10 ? ('0' + date) : date) + '/' + (month < 10 ? ('0' + month) : month) + '/' + year;
alert(newDate);
}
// Usage example
var now = new Date();
formatDate(now)
I have written these two javascript functions:
function getDateFromDateAtHourOfDay(date, hour)
{
var year = date.getFullYear();
var month = date.getMonth();
var day = date.getDate();
var newDate = new Date(year, month, day, hour, 0, 0);
return (newDate);
}
and
function getDateDescriptionFromDate(date)
{
var year = date.getFullYear();
var month = date.getMonth();
var day = date.getDate();
return ( (day < 10 ? ("0" + day) : (day)) + "." + (month < 10 ? ("0" + month) : (month)) + "." + year);
}
The first one should return a new date with the same year/month/day but with a different hour of the day (e.g. switch 2015-04-05 15:00 to 2015-04-05 16:00).
The second one should just return a date-string in the format dd.MM.yyyy.
Now if I call
var selectedDate = new Date(); // normally function parameter
var startDate = getDateFromDateAtHourOfDay(selectedDate, hour);
document.getElementById("dateLabel").innerHTML = getDateDescriptionFromDate(startDate);
Where hour is a function parameter and for example 15 (tested with alert), in my "dateLabel" it says 05.01.2015. But if I do
alert (selectedDate);
the result is: Sun Apr 05 2015 15:52:26 GMT+0200 (CEST) => now.
selectedDate is not modified between the calls (alert and set the innerHTML).
I think the two functions do not do what I suppose them to, but maybe you find the mistake.
Thank you !
EDIT:
I tried this code:
selectedDate = new Date();
alert(selectedDate); // Sun Apr 05 2015 16:36:07 GMT+0200 (CEST)
var startDate = getDateFromDateAtHourOfDay(selectedDate, hour);
alert(hour); // 8
alert(startDate); // Thu Mar 05 2015 08:00:00 GMT+0100 (CET)
document.getElementById("datumLabel").innerHTML = getDateDescriptionFromDate(startDate); // 05.01.2015
I don't know why you're surprised by what selectedDate is returning. You have only set it to new Date() (now). Nothing in the code is manipulating this variable from the point of creation.
You are, however, manipulating the value and storing the change in startDate and dateLabel.innerHTML. You would only notice the formatting though since the second function strips any change in "time" (done by the first function).
So, in short: You create a date (now), change the time, then format it to only show date.
var selectedDate = new Date();
//selectedDate value = the date and time right now
var startDate = getDateFromDateAtHourOfDay(selectedDate, hour);
//startDate value = whatever selectedDate was + hours sent as parameter
document.getElementById("dateLabel").innerHTML = getDateDescriptionFromDate(startDate);
//Formatted value of startDate, to only show date
As per comment:
You're missing that month in javascript is zero-based. So you'll have to do something like:
var month = date.getMonth() + 1; //in the second function
function getDateFromDateAtHourOfDay(date, hour)
{
var year = date.getFullYear();
var month = date.getMonth();
var day = date.getDate();
var newDate = new Date(year, month, day, hour, 0, 0);
return (newDate);
}
function getDateDescriptionFromDate(date)
{
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
return ( (day < 10 ? ("0" + day) : (day)) + "." + (month < 10 ? ("0" + month) : (month)) + "." + year);
}
var selectedDate = new Date(); // normally function parameter
var startDate = getDateFromDateAtHourOfDay(selectedDate, 15);
document.getElementById("dateLabel").innerHTML = 'Your current system date: ' + getDateDescriptionFromDate(startDate);
<div id="dateLabel"></div>
If you try this, this should give you save value for your alert and Label, the value you are using for the Label is different than the value you are trying to use alert on.
selectedDate = new Date();
alert(selectedDate); // Sun Apr 05 2015 16:36:07 GMT+0200 (CEST)
var startDate = getDateFromDateAtHourOfDay(selectedDate, hour);
alert(hour); // 8
alert(startDate); // Thu Mar 05 2015 08:00:00 GMT+0100 (CET)
var startDateDesc = getDateDescriptionFromDate(startDate);
alert(startDateDesc) // 05.01.2015
document.getElementById("datumLabel").innerHTML = startDateDesc; // 05.01.2015
I'm working with a string of data in this format: "mm-dd-yy". I convert this to a Date object in this way:
var dateData, dateObject, dateReadable, dateSplit, year, month, day;
dateData = "07-21-14"; //For example
dateSplit = dateData.split('-');
month = dateSplit[0] - 1;
day = dateSplit[1];
year = 20 + dateSplit[2];
dateObject = new Date(year, month, day);
dateReadable = dateObject.toUTCString(); //Returns Mon, 21 Jul 2014 04:00:00 GMT
I would like to return the date (Mon, 21 Jul 2014) without the time (04:00:00 GMT). Is there a different method that will do so? Or a way of calling .toUTCString() to return the date without the time?
I believe you want .toDateString() or .toLocaleDateString()
http://www.w3schools.com/jsref/jsref_todatestring.asp
In fact, you should also look at Date.parse():
var dateData, dateObject, dateReadable;
dateData = "07-21-14"; //For example
dateObject = new Date(Date.parse(dateData));
dateReadable = dateObject.toDateString();