Digits with leading zero in date and time - javascript

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

Related

Can not generate Date and Time on Website using javascript

I am trying to add a date and time to the footer of my page. I tried everything and can generate the date a time.
function currentDateTime() {
var x = new Date()
var x1=x.getMonth() + 1+ "/" + x.getDate() + "/" + x.getFullYear();
x1 = x1 + " - " + x.getHours( )+ ":" + x.getMinutes() + ":" + x.getSeconds();
document.getElementById('currentDateTime').innerHTML = x1;
currentDateTime();
}
<footer>
Policy and Terms
FAQ
<p><span id="currentDateTime"> </span></p>
</footer>
You have to call your function currentDateTime() outside of itself
function currentDateTime() {
var x = new Date()
var x1=x.getMonth() + 1+ "/" + x.getDate() + "/" + x.getFullYear();
x1 = x1 + " - " + x.getHours( )+ ":" + x.getMinutes() + ":" + x.getSeconds();
document.getElementById('currentDateTime').innerHTML = x1;
}
currentDateTime();
<footer>
Policy and Terms
FAQ
<p><span id="currentDateTime"> </span></p>
</footer>
you call function inside it
function currentDateTime() {
var x = new Date()
var x1= x.getMonth() + 1+ "/" + x.getDate() + "/" + x.getFullYear();
x1 = x1 + " - " + x.getHours( )+ ":" + x.getMinutes() + ":" + x.getSeconds();
document.getElementById('currentDateTime').innerHTML = x1;
}
currentDateTime();

How to enter newline character in bookmarklet JavaScript

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

Update date on click without refreshing the page

I wrote this code:
<script type="text/javascript">
$('#updateAssignment').on('click', function(event){
event.preventDefault();
var currentdate = new Date();
var datetime = "Last Sync: " + currentdate.getDate() + "/"
+ (currentdate.getMonth()) + "/"
+ currentdate.getFullYear() + " # "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds();
$('#updateAssignment').text(datetime);
});
</script>
With this html:
<p class="card-text" id="updateAssignment">Last update: <?php echo date('d-m-Y H:i:s')?> <i class="fa fa-refresh"></i></p>
What I want is the following:
I want the date to update when I click on the refresh button without the complete page to refresh. Somehow it doesn't work with this code, I don't have any error's in the console.
Could anyone help me out?
Replace
href="?update=true"
With
href="#"
The previous line makes your browser initiate a GET request, which you don't want.
You can achieve your need at multi ways, below added the one way with your code.
you added event.preventDefault(); so click event prevented.
or you can remove href link as per BeetleJuice suggest.
Html
<p class="card-text" id="updateAssignment">
<span>Last update: <?php echo date('d-m-Y H:i:s')?></span>
<i class="fa fa-refresh"></i>Sync Date
</p>
Scripts
$('#updateAssignment a').on('click', function(event){
event.preventDefault();
var currentdate = new Date();
var datetime = "Last Sync: " + currentdate.getDate() + "/"
+ (currentdate.getMonth()) + "/"
+ currentdate.getFullYear() + " # "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds();
$('#updateAssignment span').text(datetime);
});
DEMO
This works :
$('#updateAssignment').click(function(){
var currentdate = new Date();
var datetime = "Last Sync: " + currentdate.getDate() + "/"
+ (currentdate.getMonth()) + "/"
+ currentdate.getFullYear() + " # "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds();
$('#updateAssignment').text(datetime);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="card-text" id="updateAssignment">Last update: PHP_HERE <i class="fa fa-refresh"></i></p>
Replace
href="?update=true"
to
href="#null"
and
$(function() {
$('#updateAssignment').on('click', function(event){
event.preventDefault();
var currentdate = new Date();
var datetime = "Last Sync: " + currentdate.getDate() + "/"
+ (currentdate.getMonth()) + "/"
+ currentdate.getFullYear() + " # "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds();
$('#updateAssignment').text(datetime);
});
});
Wrap it by $(function() {

how to get current time in razor view using javascript

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..

Javascript Date Function Not working

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

Categories

Resources