Add date change script - javascript

I have this code that shows the date of the next day. Is it possible to change the behavior of the dates so it changes at 17:00? For example, now the code shows the date 16.6.2020 (this is correct), and after 17:00, it should show 17.6.2020
document.querySelector("a[href='data']").setAttribute("id", "day");
document.querySelector("a[href='data2']").setAttribute("id", "day2");
var currentDate = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
var day = currentDate.getDate()
var month = currentDate.getMonth() + 1
var year = currentDate.getFullYear()
document.getElementById("day").innerHTML = day + "." + month + "." + year;
document.getElementById("day2").innerHTML = day + "." + month + "." + year;
<a class="tn-atom" href="data" id=""></a>
<a class="tn-atom" href="data2" id=""></a>

You mean this (comment currentDate.setHours(18) after test)
const pad = num => ("0"+num).slice(-2)
document.querySelector("a[href='data']").setAttribute("id", "day");
document.querySelector("a[href='data2']").setAttribute("id", "day2");
var currentDate = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
currentDate.setHours(18); // comment this when tested
if (currentDate.getHours() >=17) currentDate.setDate(currentDate.getDate()+1)
var day = currentDate.getDate()
var month = currentDate.getMonth() + 1
var year = currentDate.getFullYear()
document.getElementById("day").innerHTML = pad(day) + "." + pad(month) + "." + year;
document.getElementById("day2").innerHTML = pad(day) + "." + pad(month) + "." + year;
<a class="tn-atom" href="data" id=""></a>
<a class="tn-atom" href="data2" id=""></a>

If you don't feel like messing with time zones, add 7 hours to the date for it to show the next days date at 17:00
var currentDate = new Date(new Date().getTime() + 31 * 60 * 60 * 1000);
console.log(currentDate)
// testing DST
const aroundDST = new Date(2020,2,29,0,0,0,0); // 29th of March 2020 - DST-SST in most of Europe
const afterDST = new Date(aroundDST.getTime() + 31 * 60 * 60 * 1000);
console.log(afterDST)

Related

Why this code got an Uncaught TypeError

I want to get next day and format it into "yyyy-MM-dd HH:mm" format, but when I run this in chrome's console, I got an Uncaught TypeError: date.getHours is not a function, why? The nextDay variable is clearly an instance of Date.
But when I removed hour and minute, just kept year, month and date, it successed, can anyone tell me the reason?
var time = new Date().getTime();
var interval = 24 * 60 * 60 * 1000;
var nextDay = new Date(time + interval);
function padding(number) {
return number < 10 ? "0" + number : "" + number;
}
function format(date) {
var year = date.getFullYear(),
month = date.getMonth(),
date = date.getDate(),
hour = date.getHours(),
minute = date.getMinutes();
return padding(year) + "-"
+ padding(month + 1) + "-"
+ padding(date) + " "
+ padding(hour) + ":"
+ padding(minute);
}
console.log(format(nextDay));
Your function takes a parameter named "date" and then tries to declare a local variable named "date". That declaration will be ignored, and the initializer will just overwrite the value of the parameter.
Change the name of the parameter:
function format(d) {
var year = d.getFullYear(),
month = d.getMonth(),
date = d.getDate(),
hour = d.getHours(),
minute = d.getMinutes();
return padding(year) + "-"
+ padding(month + 1) + "-"
+ padding(date) + " "
+ padding(hour) + ":"
+ padding(minute);
}
You are using the same variable name as the parameter, date is used twice, change the variable name like down below.
var time = new Date().getTime();
var interval = 24 * 60 * 60 * 1000;
var nextDay = new Date(time + interval);
function padding(number) {
return number < 10 ? "0" + number : "" + number;
}
function format(date) {
var year = date.getFullYear(),
month = date.getMonth(),
theDate = date.getDate(), //change the variable name
hour = date.getHours(),
minute = date.getMinutes();
return padding(year) + "-"
+ padding(month + 1) + "-"
+ padding(date) + " "
+ padding(hour) + ":"
+ padding(minute);
}
console.log(format(nextDay));

Javascript multiple work hours calculate from date to now

I need a javascript like this one
// Calculate days since 7st May 2016
var initialDate = new Date(2016, 5, 7);
var now = Date.now();
var difference = now - initialDate;
var millisecondsPerDay = 24 * 60 * 60 * 1000;
var daysSince = Math.floor(difference / millisecondsPerDay);
// Write result to HTML
document.getElementById('days_since').innerHTML = daysSince;
<div id="days_since"></div>
but I would like to display hours and my work is eg. monday-friday from 10am to 6pm and every saturday from 11am to 3pm.
Any advice?
Hi please refer this http://jsfiddle.net/yentup/fKA9X/1/
JS
$(function(){
var calcNewYear = setInterval(function(){
date_future = new Date(new Date().getFullYear() +1, 0, 1);
date_now = new Date();
seconds = Math.floor((date_future - (date_now))/1000);
minutes = Math.floor(seconds/60);
hours = Math.floor(minutes/60);
days = Math.floor(hours/24);
hours = hours-(days*24);
minutes = minutes-(days*24*60)-(hours*60);
seconds = seconds-(days*24*60*60)-(hours*60*60)-(minutes*60);
$("#time").text("Time until new year:\nDays: " + days + " Hours: " + hours + " Minutes: " + minutes + " Seconds: " + seconds);
},1000);
});
and HTML
<div id="time"></div>

Convert UNIX timestamp to date time (javascript)

Timestamp:
1395660658
Code:
//timestamp conversion
exports.getCurrentTimeFromStamp = function(timestamp) {
var d = new Date(timestamp);
timeStampCon = d.getDate() + '/' + (d.getMonth()) + '/' + d.getFullYear() + " " + d.getHours() + ':' + d.getMinutes();
return timeStampCon;
};
This converts the time stamp properly in terms of time format, but the date is always:
17/0/1970
Why - cheers?
You have to multiply by 1000 as JavaScript counts in milliseconds since epoch (which is 01/01/1970), not seconds :
var d = new Date(timestamp*1000);
Reference
function convertTimestamp(timestamp) {
var d = new Date(timestamp * 1000), // Convert the passed timestamp to milliseconds
yyyy = d.getFullYear(),
mm = ('0' + (d.getMonth() + 1)).slice(-2), // Months are zero based. Add leading 0.
dd = ('0' + d.getDate()).slice(-2), // Add leading 0.
hh = d.getHours(),
h = hh,
min = ('0' + d.getMinutes()).slice(-2), // Add leading 0.
ampm = 'AM',
time;
if (hh > 12) {
h = hh - 12;
ampm = 'PM';
} else if (hh === 12) {
h = 12;
ampm = 'PM';
} else if (hh == 0) {
h = 12;
}
// ie: 2014-03-24, 3:00 PM
time = yyyy + '-' + mm + '-' + dd + ', ' + h + ':' + min + ' ' + ampm;
return time;
}
You can get the value by calling like convertTimestamp('1395660658')
Because your time is in seconds. Javascript requires it to be in milliseconds since epoch. Multiply it by 1000 and it should be what you want.
//time in seconds
var timeInSeconds = ~(new Date).getTime();
//invalid time
console.log(new Date(timeInSeconds));
//valid time
console.log(new Date(timeInSeconds*1000));
const timeStamp = 1611214867768;
const dateVal = new Date(timeStamp).toLocaleDateString('en-US');
console.log(dateVal)

add two hours to a given date

The following code is used to get a date value and then add two hours to it. I would really like to know how to give a condition to add that two hours only between 08:30 in the morning until 18:30 of the evening and skip both the days ( Saturday,Sunday ).
For example: if the given date was in 17:30 of Tuesday so it followed the rule it will be ( 17:30 (of Tuesday ) + 2 = 09:30 ( of Wednesday-the next day) and if the given date was in 17:00 (of Friday) it will be if we skip the week-ends 09:00 ( of Monday-skip week-ends ) etc...
var now = new Date(Date.parse($(".x-form-hidden.x-form-field :eq(0)").val()));
now.setHours(now.getHours() + 2);
var dayVar = "";
if (now.getDate() < 10) {
dayVar = 0 + "" + now.getDate()
} else {
dayVar = now.getDate();
}
var dateString =
now.getFullYear() + "-" +
(now.getMonth() + 1) + "-" +
dayVar + " " +
now.getHours() + ":" + now.getMinutes() + ":0" + now.getSeconds();
$(".x-form-hidden.x-form-field :eq(1)").attr('value', dateString);
function addTwoHours(date) {
//date to decimal
var day = date.getDay() - 1;
var hour = date.getHours() - 8;
var decimal = day * 10 + hour;
//add two hours
decimal += 2;
//decimal to date
var newDay = Math.floor(decimal / 10);
var nextDay = newDay - day == 1;
var weekEnd = newDay % 5 == 0;
var newHour = (decimal % 10) + 8;
var newDate = new Date(date.getTime() + (nextDay?24:0) * (weekEnd?3:1) * 60 * 60 * 1000);
newDate.setHours(newHour);
return newDate;
}
Your time range represent 10 hours per day for 5 days a week, so it's easy to map to a continuous block of 50 hours. Considering then the units and tens gives the shift.
The following example gets the date for Friday 1 Feb. 17:15, which returns Monday 4 Feb 9:15. addTwoHours(new Date(2013, 1, 1, 17, 15));

javascript date + 7 days

What's wrong with this script?
When I set my clock to say 29/04/2011 it adds 36/4/2011 in the week input! but the correct date should be 6/5/2011
var d = new Date();
var curr_date = d.getDate();
var tomo_date = d.getDate()+1;
var seven_date = d.getDate()+7;
var curr_month = d.getMonth();
curr_month++;
var curr_year = d.getFullYear();
var tomorrowsDate =(tomo_date + "/" + curr_month + "/" + curr_year);
var weekDate =(seven_date + "/" + curr_month + "/" + curr_year);
{
jQuery("input[id*='tomorrow']").val(tomorrowsDate);
jQuery("input[id*='week']").val(weekDate);
}
var date = new Date();
date.setDate(date.getDate() + 7);
console.log(date);
And yes, this also works if date.getDate() + 7 is greater than the last day of the month. See MDN for more information.
Without declaration
To return timestamp
new Date().setDate(new Date().getDate() + 7)
To return date
new Date(new Date().setDate(new Date().getDate() + 7))
Something like this?
var days = 7;
var date = new Date();
var res = date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
alert(res);
convert to date again:
date = new Date(res);
alert(date)
or alternatively:
date = new Date(res);
// hours part from the timestamp
var hours = date.getHours();
// minutes part from the timestamp
var minutes = date.getMinutes();
// seconds part from the timestamp
var seconds = date.getSeconds();
// will display time in 10:30:23 format
var formattedTime = date + '-' + hours + ':' + minutes + ':' + seconds;
alert(formattedTime)
In One line:
new Date(Date.now() + 7 * 24 * 60 * 60 * 1000)
The simple way to get a date x days in the future is to increment the date:
function addDays(dateObj, numDays) {
return dateObj.setDate(dateObj.getDate() + numDays);
}
Note that this modifies the supplied date object, e.g.
function addDays(dateObj, numDays) {
dateObj.setDate(dateObj.getDate() + numDays);
return dateObj;
}
var now = new Date();
var tomorrow = addDays(new Date(), 1);
var nextWeek = addDays(new Date(), 7);
alert(
'Today: ' + now +
'\nTomorrow: ' + tomorrow +
'\nNext week: ' + nextWeek
);
Using the Date object's methods will could come in handy.
e.g.:
myDate = new Date();
plusSeven = new Date(myDate.setDate(myDate.getDate() + 7));
var days = 7;
var date = new Date();
var res = date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
var d = new Date(res);
var month = d.getMonth() + 1;
var day = d.getDate();
var output = d.getFullYear() + '/' +
(month < 10 ? '0' : '') + month + '/' +
(day < 10 ? '0' : '') + day;
$('#txtEndDate').val(output);
var future = new Date(); // get today date
future.setDate(future.getDate() + 7); // add 7 days
var finalDate = future.getFullYear() +'-'+ ((future.getMonth() + 1) < 10 ? '0' : '') + (future.getMonth() + 1) +'-'+ future.getDate();
console.log(finalDate);
You can add or increase the day of week for the following example and hope this will helpful for you.Lets see....
//Current date
var currentDate = new Date();
//to set Bangladeshi date need to add hour 6
currentDate.setUTCHours(6);
//here 2 is day increament for the date and you can use -2 for decreament day
currentDate.setDate(currentDate.getDate() +parseInt(2));
//formatting date by mm/dd/yyyy
var dateInmmddyyyy = currentDate.getMonth() + 1 + '/' + currentDate.getDate() + '/' + currentDate.getFullYear();
Two problems here:
seven_date is a number, not a date. 29 + 7 = 36
getMonth returns a zero based index of the month. So adding one just gets you the current month number.

Categories

Resources