I am working on a datepicker calendar for adding a previous and next date feature, a button on click. I tried in PHP and JS and I did not have success on both.
window.onload = function () {
let today = new Date();
let D = String(today.getDate()).padStart(2, '0');
let M = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
let YYYY = today.getFullYear();
today = YYYY + '-' + M + '-' + D;
let today2 = new Date();
today2 = M + "/" + D + "/" + YYYY;
var prevday = new Date();
prevday.setDate(prevday.getDate() - 1);
var nextday = new Date();
nextday.setDate(nextday.getDate() + 1)
addRow('<tr><td colspan="2"><h3>Eventsz for '+ ' ' + today2 + ' ' + '<a href="#" id="prev" class="fa fa-chevron-right lg" title="Next Date"></h3></td></tr>');
Don't know if its a typo but in your snippet you are missing a trailing }.
window.onload = function () {
let today = new Date();
let D = String(today.getDate()).padStart(2, '0');
let M = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
let YYYY = today.getFullYear();
today = YYYY + '-' + M + '-' + D;
let today2 = new Date();
today2 = M + "/" + D + "/" + YYYY;
var prevday = new Date();
prevday.setDate(prevday.getDate() - 1);
var nextday = new Date();
nextday.setDate(nextday.getDate() + 1)
} // Missing
// addRow('<tr><td colspan="2"><h3>Eventsz for '+ ' ' + today2 + ' ' + '<a href="#" id="prev" class="fa fa-chevron-right lg" title="Next Date"></h3></td></tr>');
I try to write a bookmarklet which creates email, but there is an issue about using newline character.
When I execute the below code from Chrome Console, it works fine. However, this code doesn't work when executing from bookmarklet. I checked the code and the cause seems var body1 includes newline character(%0D%0A).
Anybody knows how to insert newline character in bookmarklet JS?
javascript:(function(){
var today = new Date();
var url = 'https://mail.google.com/mail/?view=cm';
var to = 'emailfrombookmarklet#example.com';
var subject = '【weather%20report】【' + today.getFullYear() + '-' + ("00" + (today.getMonth() + 1)).slice(-2) + '-' + ("00" + today.getDate()).slice(-2) + '%20bar】%20email%20from%20bookmarklet';
var body1 = 'Hi%2C%0D%0A%0D%0AThis is Kim Kardashian%2E%0D%0AIt%27s%20sunny%20today%2E%0D%0A%0D%0Adate%3A';
var targetDate = '%20' + today.getFullYear() + '-' + ("00" + (today.getMonth() + 1)).slice(-2) + '-' + ("00" + today.getDate()).slice(-2);
var body2 = '%0D%0AName%3A%20Kim%20Kardashian%0D%0A';
var body3 = '%0D%0ABest%20Regards%2C';
url += '&to=' + to + '&su=' + subject + '&body=' + body1 + targetDate + body2 + body3;
window.open(url);})();
It would be better to get rid of using encoded characters at all:
javascript:(function() {
var today = new Date();
var url = 'https://mail.google.com/mail/?view=cm';
var to = 'emailfrombookmarklet#example.com';
var subject = '【weather report】【' + today.getFullYear() + '-' + ('00' + (today.getMonth() + 1)).slice(-2) + '-' + ('00' + today.getDate()).slice(-2) + ' bar】 email from bookmarklet';
var body1 = 'Hi,\n\nThis is Kim Kardashian.\nIt\'s sunny today.\n\ndate:';
var targetDate = ' ' + today.getFullYear() + '-' + ('00' + (today.getMonth() + 1)).slice(-2) + '-' + ('00' + today.getDate()).slice(-2);
var body2 = '\nName: Kim Kardashian\n';
var body3 = '\nBest Regards,';
url += '&to=' + encodeURIComponent(to) + '&su=' + encodeURIComponent(subject) + '&body=' + encodeURIComponent(body1 + targetDate + body2 + body3);
window.open(url);
})();
I found this Javascript script online which refreshes the time every second and displays it. I want the program to display digits with leading zero. Currently it's showing 6/8/2017 - 19:8:54 but I want it to show 06/08/2017 - 19:08:54.
<script type="text/javascript">
function display_c(){
var refresh=1000; // Refresh rate in milli seconds
mytime =setTimeout('display_ct()',refresh)
}
function display_ct() {
var strcount
var x = new Date()
var x1=x.getDate() + "/" + x.getMonth() + "/" + x.getYear();
x1 = x1 + " - " + x.getHours( )+ ":" + x.getMinutes() + ":" +
x.getSeconds();
document.getElementById('ct').innerHTML = x1;
tt=display_c();
}
</script>
You can use the following:
var x1 = ('0' + x.getDate()).slice(-2) + '/'
+ ('0' + (x.getMonth()+1)).slice(-2) + '/'
+ x.getFullYear() + '-'
+ ('0' + x.getHours()).slice(-2) + ':'
+ ('0' + x.getMinutes()).slice(-2) + ':'
+ ('0' + x.getSeconds()).slice(-2);
I have tried many ways mentioned below but none of them is working
var currentTime = Date.parse(date + ' ' + currentDate.getHours() + ':' + currentDate.getMinutes() + ':' + currentDate.getSeconds());
var t = DateTime.Now.ToString("h:mm:ss tt")
DateTime.Now.TimeOfDay etc
thanks in advance
try using this function..
function $dateTime() {
var d = new Date();
var dd = d.getDate();
var mm = d.getMonth()+1;//January is 0!
var yyyy = d.getFullYear();
var h=d.getHours();
var m=d.getMinutes();
var s=d.getSeconds();
if(dd<10){dd='0'+dd}
if(mm<10){mm='0'+mm}
if(h<10){h='0'+h}
if(m<10){m='0'+m}
if(s<10){s='0'+s}
return yyyy + '-' + mm + '-' + dd + ' ' + h + ':' + m + ':' + s;
}
returns the value 2015-01-06 and current time..
Hi I am trying to add 31 days to 'myDate' which is the current date. It is supposed to get the date add 31 days, then the convertDate function is supposed to translate it to something like 'Nov 31, 2012'. But it doesn't work. Does anyone know why?
Here is the primary function...
function process (infoarray) {
var myDate = new Date();
//var final = convertDate(myDate);
var length = infoarray.length;
var final_string;
for (var b = 0; b < length; b++) {
if (b == 0) {
if (infoarray[b][3] == 'After') {
final_string = '<b>' + infoarray[b][3] + ' ' + infoarray[b][1] + '</b><br/>' + infoarray[b][0] + '<br/>';
} else {
final_string = '<b>' + infoarray[b][1] + ' ' + infoarray[b][3] + ' ' + infoarray[b][2] + '</b><br/>' + infoarray[b][0] + '<br/>';
}
} else {
if (infoarray[b][3] == 'After') {
final_string = final_string + '<br/><b>' + infoarray[b][3] + ' ' + convertDate(myDate.setDate(myDate.getDate() + 31)) + '</b><br/>' + infoarray[b][0] + '<br/>';
} else {
final_string = final_string + '<br/><b>' + infoarray[b][1] + ' ' + infoarray[b][3] + ' ' + infoarray[b][2] + '</b><br/>' + infoarray[b][0] + '<br/>';
}
}
}
return final_string;
}
Here is the line i am focused on from the function above...
final_string = final_string + '<br/><b>' + infoarray[b][3] + ' ' + convertDate(myDate.setDate(myDate.getDate() + 31)) + '</b><br/>' + infoarray[b][0] + '<br/>';
Here is the convertDate function...
function convertDate(d) {
var day = d.getDate();
if (day < 10) {
day = '0' + day;
}
var year = d.getFullYear();
var month = d.getMonth();
var months=['Jan','Feb','Mar','Apr','May','June','July','Aug','Sep','Oct', 'Nov','Dec'];
var currentMonth = months[month];
return (currentMonth + ' ' + day + ', ' + year);
}
myDate.setDate(...) modifies the value of the Date instance, but doesn't return anything.
You need to call setDate first, then pass the variable to your function.
Here.
First calculate then call
DEMO;
var myDate = new Date();
myDate.setDate(myDate.getDate()+31);
final_string = final_string + '<br/><b>' +
infoarray[b][3] + ' ' +
convertDate(myDate) + '</b><br/>' + infoarray[b][0] + '<br/>';
Or add it to the function:
.... convertDate(myDate,31) + ....
With
function convertDate(d,offset) {
if ( offset ) d.setDate(d.getDate()+offset);
var day = d.getDate();
if (day < 10) {
day = '0' + day;
}
var year = d.getFullYear();
var month = d.getMonth();
var months=['Jan','Feb','Mar','Apr','May','June','July','Aug','Sep','Oct', 'Nov','Dec'];
var currentMonth = months[month];
return (currentMonth + ' ' + day + ', ' + year);
}
DEMO
By using setDate, you tell the Date object to set the day number to something between 32 and 62, which doesn't make much sense.
A good way to add 31 days would be to use getTime, which returns the number of mseconds ellapsed since 1st Jan 1970:
myDate.setTime( myDate.getTime()+31*24*60*60*1000 );
//31Days x 24 hours x 60 minutes x 60 seconds x 1000 msecs