new Date can't give me right time - javascript

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);

Related

getDate returning the next day on current time

Is this due to the time zones settings on my laptop or is it something more complex?
Current time
var dateObj = new Date();
var month = dateObj.getUTCMonth() + 1; //months from 1-12
var day = dateObj.getUTCDate();
var year = dateObj.getUTCFullYear();
var week = 7;
console.log ('date ',dateObj,' day is ',day);
output
> date Mon Jan 14 2019 23:05:35 GMT-0500 (Eastern Standard Time) day is 15
edit: that the time that new Date() created (Mon Jan 14 2019 23:05:35 GMT-0500) is in fact the correct time I am after.
after considering the information I read in comments, it seems I need to subtract the hourly change ( - 5 ) to get the EST, which seems to be what I'm after.
it seems I need to subtract the hourly change ( - 5 ) to get the EST
Subtracting 5 is not a good idea, as it doesn't account for daylight savings.
I'd suggest using toLocaleString() to be safe.
var dateObj = new Date();
//Output as UTC
var utc = { timeZone: "UTC" };
console.log(dateObj.toLocaleString("en-US", utc));
//Output as EST
var est = { timeZone: "America/New_York" };
console.log(dateObj.toLocaleString("en-US", est));
You need to keep timezones consistent. Change this:
console.log ('date ',dateObj,' day is ',day);
To this:
console.log ('date ',dateObj.getUTCDate(),' day is ',day);
And it will work:
var dateObj = new Date();
var month = dateObj.getUTCMonth() + 1; //months from 1-12
var day = dateObj.getUTCDate();
var year = dateObj.getUTCFullYear();
var week = 7;
console.log('date ', dateObj.getUTCDate(), ' day is ', day);

Javascript Date Modifiying

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

JS Date object - return human readable string without time

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();

Adding one day to a date border case?

I don't understand why this code doesn't work. Solutions on SO mention that doing date.getDate()+1 should add a day but in my case it adds one month and two days?
var year = 2014;
var month = 3;
var day = 31;
// creating an actual date
requestedDate = new Date(year, month - 1, day);
console.debug(requestedDate.toString());
// outputs "Mon Mar 31 2014 00:00:00 GMT+0200 (CEST)"
var d = new Date();
d.setDate(requestedDate.getDate()+1);
console.debug(d.toString());
// outputs "Fri May 02 2014 11:04:52 GMT+0200 (CEST)"
You're not setting the second date to the same as the first date.
In the first new Date() you're setting the date to 31. march.
The second new Date() sets the date to today, 1. april.
31 + 1 = 32, and 1. april plus 32 days should be 2. may.
var year = 2014;
var month = 3;
var day = 31;
// creating an actual date
requestedDate = new Date(year, month - 1, day);
console.debug(requestedDate.toString());
var d = new Date(year, month - 1, day); // set the date to the same
d.setDate(requestedDate.getDate()+1);
console.debug(d.toString());
FIDDLE

formatting a date with variables in jQuery

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';
}

Categories