I want to make a link depend on current date.
Here is my Javascript:
<script type="text/javascript">
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
document.write(month + "" + day + "")
</script>
Above Javascript will show, example: 1124 (month/day)
I want to make this link follow the current day:
http://example.com/JS-RESULT
Today date is 24 11 2014 so the url will be like this
Today Budget
How can I make this work?
p.s: i love jquery.
JQuery Solution:
$(function(){
var currentTime = new Date();
var month = currentTime.getMonth() + 1
var day = currentTime.getDate();
$("#content").append("<a href='http://example.com/" + month + day + "'>Today Budget</a>");
});
JSFiddle: http://jsfiddle.net/ehLku1oq/
I am a little confused about your question, hopefully you are looking for one of the things below:
How to form the URL to example.com/1124?
Perform a simple string + string in javascript.
<script type="text/javascript">
<!--
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var date = month + "" + day + "";
var url = "http://example.com/" + date;
// Do what you want with the url
//-->
</script>
How "my manager just click the link and get into the current day" works?
Given that you have "one year list of links which have daily budgets", put all those webpages in the same folder as your current page which has Today Budget; or create a new folder called budgets for those daily budgets and change the url to http://example.com/budgets/1124.
My answer is very basic and I'm not sure if I got you correctly, let me know.
Related
Hello I have a XML file and a "created at" tag that stores the date and time like this
2012-09-15 02:08:46
I am trying to create a new Date object so I can easily print out the day month and year. But it doesn't like this format.
something like this
var theDate = new Date(Date.parse(storyDate));
console.log(theDate.getMonth());
theDate = theDate.getDate() + ", " + theDate.getMonth();
thanks
UPDATE: I can get it to work in Chrome but not Firefox.
UPDATE: Found the answer, thanks everybody. why the downvote? It turned out to be a reasonable question. I was missing a T.
var theDate = new Date(Date.parse(storyDate.replace(' ', 'T')));
The 'T' is required between the date and time.(at least in FireFox)
Valid DateTime formats
I would parse the date manually using regular expressions.
var dateStr = "2012-09-15 02:08:46"
var dateRegex = /^(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2}):(\d{2})$/
var dateParts = dateRegex.exec(dateStr)
var year = dateParts[1],
month = parseInt(dateParts[2], 10) - 1,
day = dateParts[3],
hour = dateParts[4],
minutes = dateParts[5],
seconds = dateParts[6];
var date = new Date(year, month, day, hour, minutes, seconds);
//
console.log(date.getMonth())
console.log(date.getDate() + ", " + date.getMonth());
Am rediscovering HTML after quite some time.
I am using just HTML and javascript along with Salesforce. I have two date input fields.
I was curious to see if there is any easy way to populate these fields with:
a. Today's date
b. Date 6 months before today.
<input type="text" id="toDate" size="10" onmouseover="initialiseCalendar(this, 'toDate')"/>
Thanks,
Calvin
The following JavaScript sets the value of your textbox to today's date, in format yyyy-mm-dd. See how I add 1 to the month? getMonth() returns 0-11 for current month, so 1 is added to it:
var today = new Date();
document.getElementById("toDate").value = today.getFullYear() + "-" +
parseInt(today.getMonth()+1) + "-" + today.getDate();
DEMO: Fiddle
It is worth noting though that if the month or day are lower than 10, you'll only get one digit for each of them. Let me know if this is an issue.
EDIT: To get 6 months from today, use:
var today = new Date();
var past = today.setMonth(today.getMonth() - 6);
Populate with today's date:
var today = new Date();
document.getElementById("toDate").value = today.getFullYear() + "-"
+ String(today.getMonth() + 101).slice(-2) + "-"
+ String(today.getDate() + 100).slice(-2);
6 month in the past:
var past = new Date();
past.setMonth(past.getMonth() - 6); //
document.getElementById("toOldDate").value = past.getFullYear() + "-"
+ String(past.getMonth() + 101).slice(-2) + "-"
+ String(past.getDate() + 100).slice(-2);
I am looking for a time output using jQuery, for example, would be great to know what time it is on the visitor's browser and the current day (friday,saturday,monday, etc...).
Is there any way to do it only with jQuery? I don't really like the way javascript handles time issues.
If you recommend any plugin, please tell me wich.
Thanks so much!
Souza.
EDIT:
I'm looking to avoid substring javascript outputs, or convert the results.
Wouldn't be great to use
$("#setime").yourtime("day");
and give me the day?
or
$("#setime").yourtime("hour", 24format);
and give you the hour in any format you need?
?
Try:
var currentTime = new Date();
It's not jQuery but it will do what you want.
You also have:
var month = currentTime.getMonth()
var day = currentTime.getDate()
var year = currentTime.getFullYear()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
To play with.
Perhaps this is what you were looking for?
http://crossbreeze.github.com/jquery-sensible-datetime/
Download here: https://github.com/crossbreeze/jquery-sensible-datetime
If not, here is plain JS for you to reuse
<script type="text/javascript">
var weekday=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday"," Saturday"];
var monthname=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
function formatDate(d) {
var text = "";
text += weekday[d.getDay()] + " ";
text += d.getDate() + " ";
text += monthname[d.getMonth()] + " ";
text += d.getFullYear();
var hh = d.getHours();
var mm = d.getMinutes();
if (hh<10) hh = "0"+hh;
if (mm<10) mm = "0"+mm;
return text +" "+hh+":"+mm;
}
$(document).ready(function () {
var d = new Date();
$("#dateDiv").text(formatDate(d));
});
</script>
I need a help..
I have a Current Date and No of days column.
When i enter number of days,i should add current date plus no of days entered.
For example,
todays date 5th jan + 20(no of days) = 25th Jan 2011 in another column.
Kindly help me.
Thanks in Advance.
Date.js is fantastic for this.
Date.today().add(5).days();
As you are learning JavaScript you may find the w3schools site useful for simple examples of objects and functions that are exposed and how they may be used.
http://www.w3schools.com/jsref/jsref_obj_date.asp
You can calculate the date as follows:
var d = new Date(); // Gets current date
var day = 86400000; // # milliseconds in a day
var numberOfDays = 20;
d.setTime(d.getTime() + (day*numberOfDays)); // Add the number of days in milliseconds
You can then use one of the various methods of displaying the date:
alert(d.toUTCString());
You could do something like
Date.today().add(X).days();
Where X is the number of days the user has entered.
You can add dates like this in js:
var someDate = new Date();
var numberOfDaysToAdd = 6;
someDate.setDate(someDate.getDate() + numberOfDaysToAdd);
var month = someDate.getMonth() + 1; //Add 1 because January is set to 0 and Dec is 11
var day = someDate.getDate();
var year = someDate.getFullYear();
document.write(month + "/" + day + "/" + year);
See this p.cambell's answer here: How to add number of days to today's date?
I want to set a text box with a date (in dd/mm/yyyy format) 14 days ahead to current date in javascript . can any one help me regarding this ?
This should do it:
var myDate=new Date();
myDate.setDate(myDate.getDate()+14);
then
document.getElementById(YOUR_TEXTBOX_ID).value = myDate.getDate() + "/" +
(myDate.getMonth() + 1) + "/" + myDate.getFullYear();
Date.js is a handy script for all kinds of JavaScript date manipulation. I've used it to make many date-based interfaces, including calendar controls.
Like Deodeus suggested, use Date.js:
var myDate = Date.today().add(14).days();
document.getElementById('mytextbox').value = myDate.toString('dd/MM/yyyy');
Following is the function to increment date by one day in javascript.
function IncrementDate(date) {
var tempDate = new Date(date);
tempDate.setDate(tempDate.getDate() + 1);
return tempDate;
}
Function calling...
var currentDate = new Date();
var IncrementedDate = IncrementDate(currentDate);