I know there are a few similar posts about this topic, but none were helping me much (or maybe that's cause I'm still a newbie).
Anyway, the thing I'm looking for is a script to show me the day and date + 11 days from now.
The script below is what I use to show the current day & date. So I figured there has to be some simple line to add or modify somewhere? like "+11"
Any helpful answer would be very very very much appreciated!
var daynames = new Array( "Sunday", "Monday", "Tuesday", "Wednesday","Thursday", "Friday", "Saturday");
var months = new Array( "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december");
var currentTime = new Date();
var dayname = currentTime.getDay();
var month = currentTime.getMonth();
var day = currentTime.getDate();
var year = currentTime.getFullYear();
document.write(daynames[dayname] + "<BR> " + day + " " + months[month] + " " + year);
Assuming you are working on JavaScript, this snippet will set d to +11 days from current day -
var d = new Date();
d.setDate(d.getDate()+11);
Assuming javascript because of document.write
var yourDate=new Date();
yourDate.setDate(yourDate.getDate()+11); //11 is the number of days you want to add
Refer: getDate and setDate
Related
I am trying get last six months date from current date .
var d = new Date();
var months = ["January","February","March","April","May","June","July","August","September","October","November","December"];
alert(months[d.getMonth()-6]);
but i am getting udefined
I always recommend using date libraries to assist in these types of calculations. One of the most popular with a ton of support and examples is moment.js (https://momentjs.com/)
To get six months ago from the current date using moment is:
moment().subtract(6, 'months')
and then to print the month name would be:
moment().subtract(6, 'months').format('MMMM')
You have to write your code like below-
var d = new Date();
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
d.setMonth(d.getMonth() - 6);
console.log(months[d.getMonth()]);
We need to get Month from date object the set to back (with - 6) then get it back.
var d = new Date();
var months = ["January","February","March","April","May","June","July","August","September","October","November","December"];
var monthName = months[new Date(d.setMonth(d.getMonth() - 6)).getMonth()];
console.log(monthName)
My js project is working fine but I need to exclude Saturdays and Sunday in it, or even holiday. Can you please check on my code on how can I achieve this.
<p>Meet me at <span id="thedate"></span></p>
<script>
var m_names = ["January", "February", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December"
];
var d_names = ["Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"
];
var myDate = new Date();
myDate.setDate(myDate.getDate() + 7);
var curr_date = myDate.getDate();
var curr_month = myDate.getMonth();
var curr_day = myDate.getDay();
document.getElementById("thedate").innerHTML = (d_names[curr_day]
+ "," + m_names[curr_month] + " " + curr_date);
</script>
You could easily find out if the day is not between monday - friday by:
// Sunday - Saturday : 0 - 6
if (curr_day < 1 || curr_day > 5) {
// do something, maybe set it to monday?
curr_day = 1
}
I have the following code that I am using to display the current date and time, and I want it always updated so I have a setInterval to update it every second. This seems to be causing the page to be taking up 25% of the CPU and the memory it takes up just keeps climbing the longer the page is up.
Is there anything I can do to improve the performance of this?
jQuery(function($){
(function update_time(){
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var dt = new Date();
var hr = dt.getHours() > 12 ? dt.getHours() - 12 : dt.getHours();
var mi = dt.getMinutes() < 10 ? "0" + dt.getMinutes() : dt.getMinutes();
var sd = dt.getSeconds() < 10 ? "0" + dt.getSeconds() : dt.getSeconds();
var div = dt.getSeconds() & 1 ? ":" : " ";
$('.hour').text(hr);
$('.minute').text(mi);
$('.second').text(sd);
$('.day').text(days[dt.getDay()]);
$('.month').text(months[dt.getMonth()]);
$('.date').text(dt.getDate());
$('.year').text(dt.getFullYear());
$('.time-divider').text(div);
setInterval(update_time, 1000);
})();
});
Use setTimeout instead of setInterval.
The comments above have done a good job explaining why, but I'll reiterate.
Your current function will schedule a new setInterval every time it is called, on top of any existing ones. After just 5 seconds you'll have 32 intervals running. Every second this number doubles.
setTimeout executes its callback function once after a set amount of time. So after one second the timeout will fire and expire, the function will execute, and a new timeout will be created. Rinse and repeat.
jQuery(function($){
(function update_time(){
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var dt = new Date();
var hr = dt.getHours() > 12 ? dt.getHours() - 12 : dt.getHours();
var mi = dt.getMinutes() < 10 ? "0" + dt.getMinutes() : dt.getMinutes();
var sd = dt.getSeconds() < 10 ? "0" + dt.getSeconds() : dt.getSeconds();
var div = dt.getSeconds() & 1 ? ":" : " ";
$('.hour').text(hr);
$('.minute').text(mi);
$('.second').text(sd);
$('.day').text(days[dt.getDay()]);
$('.month').text(months[dt.getMonth()]);
$('.date').text(dt.getDate());
$('.year').text(dt.getFullYear());
$('.time-divider').text(div);
setTimeout(update_time, 1000);
})();
});
i have a datetime parameter in a string from an JSON object
like this-
datetime:"2015-04-15 09.00.00"
my goal is to convert it to:
"15 April 09:00"
(2015 can be included as well but not necessary)
After i will append in in 2 different places, date and time.
Any suggestions? Can't seem to find any good info apart from getting a plugin.
You can parse the month like the answer here:
https://stackoverflow.com/a/1643468/2498251
var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
so for your case you can make something like this:
var d = new Date("2015-04-15 09:00:00");
var day = d.getDay();
var month = monthNames[d.getMonth()];
var year = d.getYear();
var hour = d.getHours();
var min = d.getMinutes();
var fullDatetime = day + ' ' + month + ' ' + hour + ':' + min;
Can i get DateTime string into desired format.
I want to get current Date into this format July 24th.
How can i get this format using jquery/javascript?
I know about the Jquery DateTime, but i did't find way to convert this into this July 24th format.
My code -
var CurrentDate = new Date();
CurrentDate.format("MMMM dd");
but this code always give me - July 24. How can i convert into my desired format.
Thanks for help, any help will be appreciable.
Test this in jsfiddler here http://jsfiddle.net/Pedro3M/rDW9v/4/
var currentTime = new Date();
var Month=new Array("January", "February", "March","April", "May", "June", "July", "August", "September", "October", "November", "December");
var Suffix=new Array("th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th");
var day = currentTime.getDate();
var month = currentTime.getMonth();
if (day % 100 >= 11 && day % 100 <= 13)
today = day + "th";
else
today = day + Suffix[day % 10];
alert(Month[month]+" "+ today);
Use the plugin of jQuery for datetime format.
The plugin is here: jquery-dateFormat.
I may be completely wrong, but I don't know of any jQuery plugins that support adding "rd", "th" etc. to a date. A quick google didn't get me any results.
Here's some pseudo-code to get you started:
var toAppend = 'th';
var day = CurrentDate.getDate();
switch(day){
case '1':
toAppend = 'st';
break;
case '2':
toAppend = 'nd';
//Add the rest of the special cases
}
// If date is July 24
CurrentDate += toAppend;
Jquery date formatting and a little customization will do the job.
$(document).ready(function(){
var formattedDate = $.datepicker.formatDate("MM dd", new Date());
var day = $.datepicker.formatDate("dd", new Date());
day=parseInt(day);
if(day==1)
formattedDate+="st";
else if(day==2)
formattedDate+="nd";
else if(day==3)
formattedDate+="rd";
else
formattedDate+="th";
alert(formattedDate);
});
Include Jquery & Jquery UI library to get this functionality
Check JSFiddle HERE
As mentioned in this Link, You can do it by getting date separately and then combine as you wish. See the link please.
Some Code:
<script type="text/javascript">
<!--
var m_names = new Array("January", "February", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December");
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth();
var curr_year = d.getFullYear();
document.write(curr_date + "-" + m_names[curr_month]
+ "-" + curr_year);
//YOUR FORMAT : (MMMM dd)
document.write(m_names[curr_month]+ ' ' + curr_date);
/* The last two lines above have
to placed on a single line */
//-->
</script>
Be lucky