In my app i want to navigate through certain dates. On first click i get the date i need, but on second click it stays the same.
var getDate = function() {
var d = new Date();
var month = d.getMonth()+1;
var day = d.getDate();
var yesterday = d.getDate()-1;
var tomorrow = d.getDate()+1;
var output = d.getFullYear() + '/' +
((''+month).length<2 ? '0' : '') + month + '/' +
((''+day).length<2 ? '0' : '') + day;
$('#yesterday, #tomorrow').click(function () {
if (this.id === 'yesterday') {
var output = d.getFullYear() + '/' +
((''+month).length<2 ? '0' : '') + month + '/' +
((''+day).length<2 ? '0' : '') + yesterday;
$("#today_date").text(output);
}
else if (this.id === 'tomorrow') {
var output = d.getFullYear() + '/' +
((''+month).length<2 ? '0' : '') + month + '/' +
((''+day).length<2 ? '0' : '') + tomorrow;
$("#today_date").text(output);
}
});
$("#today_date").text(output);
};
jsfiddle example here http://jsfiddle.net/7LXPq/800/
Every time you click on yesterday and tomorrow you need to update your current d date. Otherwise you are always stuck in today date.
Trying to keep as much as your original code you can refactor it like this:
var getDate = function() {
var d = new Date();
var formatDate = function () {
var month = d.getMonth()+1;
var day = d.getDate();
var output = d.getFullYear() + '/' +
((''+month).length<2 ? '0' : '') + month + '/' +
((''+day).length<2 ? '0' : '') + day;
return output;
};
$('#yesterday, #tomorrow').click(function () {
d = new Date(d);
if (this.id === 'yesterday') {
d.setDate(d.getDate() - 1);
}
else if (this.id === 'tomorrow') {
d.setDate(d.getDate() + 1);
}
$("#today_date").text(formatDate());
});
$("#today_date").text(formatDate());
};
See demo
You can use this code:
var getDate = function() {
var d = new Date();
var output = d.getFullYear() + '/' +
(d.getMonth() + 1 < 10 ? '0' : '') + (d.getMonth()+1) + '/' +
(d.getDate() < 10 ? '0' : '') + d.getDate();
$('#yesterday, #tomorrow').click(function () {
if (this.id === 'yesterday') {
d.setDate(d.getDate()-1);
var output = d.getFullYear() + '/' +
(d.getMonth() + 1 < 10 ? '0' : '') + (d.getMonth()+1) + '/' +
(d.getDate() < 10 ? '0' : '') + d.getDate();
$("#today_date").text(output);
}
else if (this.id === 'tomorrow') {
d.setDate(d.getDate()+1);
var output = d.getFullYear() + '/' +
(d.getMonth() + 1 < 10 ? '0' : '') + (d.getMonth()+1) + '/' +
(d.getDate() < 10 ? '0' : '') + d.getDate();
$("#today_date").text(output);
}
});
$("#today_date").text(output);
};
if you need the current date to be kept, you can just make a copy of the d variable before using it
Related
getDateRange = function () {
date = new Date();
var test;
selectedOption = $('#daterange').change().val()
console.log(selectedOption) // reusult 0
switch (selectedOption) {
case 0:
test = '/' + date.getFullYear() + '-' + 0 + date.getMonth() + '-' + date.getDate() + '/' + date.getFullYear() + '-' + 0 + (date.getMonth() + 1) + '-' + date.getDate()
break
}
return test
}
console.log($('#daterange').change().val()) // result 0
console.log(getDateRange()) // result "undefined". Why?
Why is the result of the switch statement always undefined?
Change your case statement to case '0' since val() returns a string.
Also: If you are not doing any other things after your switch statement except returning your test variable, you could also just return your value and get rid of var test.
getDateRange = function() {
date = new Date();
selectedOption = $('#daterange').change().val();
switch (selectedOption) {
case '0':
return '/' + date.getFullYear() + '-' + 0 + date.getMonth() + '-' + date.getDate() + '/' + date.getFullYear() + '-' + 0 + (date.getMonth() + 1) + '-' + date.getDate();
}
}
console.log(getDateRange());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="daterange" value="0" />
$('#daterange').change().val() returns a string, but in switch you compare with a number.
Also, in the current example, you don't need switch, because you have only one case:
getDateRange = function () {
var selectedOption = $('#daterange').change().val();
if (selectedOption === '0') {
return ...
}
}
I javascript your dataType should be tighly match with your comparision operatot, so compare it with ===
In this case type and data both will match together.
getDateRange = function() {
date = new Date();
selectedOption = $('#daterange').change().val();
switch (selectedOption) {
case '0':
return '/' + date.getFullYear() + '-' + 0 + date.getMonth() + '-' + date.getDate() + '/' + date.getFullYear() + '-' + 0 + (date.getMonth() + 1) + '-' + date.getDate();
}
}
console.log(getDateRange());
I have a script that prints the current date and time in JavaScript, but when it prints time, it's missing one 0. Here is the code:
var currentdate = new Date();
var datetime = "0" + currentdate.getDate() + ".0"
+ (currentdate.getMonth()+1) + "."
+ currentdate.getFullYear() + " "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes();
document.write(datetime);
It should print 04.03.2016 15:04 and prints 04.03.2016 15:4.
Two digit minutes print fine.
Any leads?
Try this
var formatDateDigit = function (i) {
return i <= 9 ? ("0" + i) : i;
};
var currentdate = new Date();
var datetime = formatDateDigit(currentdate.getDate()) + "."
+ formatDateDigit(currentdate.getMonth()+1) + "."
+ currentdate.getFullYear() + " "
+ formatDateDigit(currentdate.getHours()) + ":"
+ formatDateDigit(currentdate.getMinutes());
document.getElementById('my_output_here').innerHTML = datetime;
<div id="my_output_here"></div>
I am trying to set Label text property based on the selection of dropdownlist data. I have a label control which stores current date and I am selecting number of days from dropdownlist to be added with current date and display the new date within another label control. While doing this I am getting
Uncaught ReferenceError: findDate is not defined.
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="http://cdn.jsdelivr.net/json2/0.1/json2.js"></script>
<script type="text/javascript">
function findDate() {
var selectvalid = $("select[id$=ddlvalid]").val();
if (selectvalid == "Select Validity") {
alert("Please Select Validity");
}
else if (selectvalid == "Upto 7 Days") {
var today = new Date();
var tomorrow = new Date(today);
tomorrow.setDate(today.getDate() + 7);
var dd = tomorrow.getDate();
var mm = tomorrow.getMonth() + 1;
var y = tomorrow.getFullYear();
var output = tomorrow.getFullYear() + '/' + (mm < 10 ? '0' : '') + mm + '/' + (dd < 10 ? '0' : '') + dd;
$("span[id$=lblenddt]").text(output);
}
else if (selectvalid == "Upto 15 Days") {
var today = new Date();
var tomorrow = new Date(today);
tomorrow.setDate(today.getDate() + 15);
var dd = tomorrow.getDate();
var mm = tomorrow.getMonth() + 1;
var y = tomorrow.getFullYear();
var output = tomorrow.getFullYear() + '/' + (mm < 10 ? '0' : '') + mm + '/' + (dd < 10 ? '0' : '') + dd;
document.getElementById("<%=lblenddt.ClientID%>").innerHTML = output;
}
else if (selectvalid == "Upto 30 Days") {
var today = new Date();
var tomorrow = new Date(today);
tomorrow.setDate(today.getDate() + 30);
var dd = tomorrow.getDate();
var mm = tomorrow.getMonth() + 1;
var y = tomorrow.getFullYear();
var output = tomorrow.getFullYear() + '/' + (mm < 10 ? '0' : '') + mm + '/' + (dd < 10 ? '0' : '') + dd;
document.getElementById("<%=lblenddt.ClientID%>").innerHTML = output;
}
};
window.onload = function() {
var d = new Date();
var month = d.getMonth() + 1;
var day = d.getDate();
var output = d.getFullYear() + '/' + (month < 10 ? '0' : '') + month + '/' + (day < 10 ? '0' : '') + day;
document.getElementById("<%=lblenqmdon.ClientID%>").innerHTML = output;
};
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>Enquiry Made On:</td>
<td>
<label id="lblenqmdon" runat="server"></label>
</td>
</tr>
<tr>
<td>Enquiry Validity:</td>
<td><asp:DropDownList ID="ddlvalid" runat="server" ClientIDMode="static" onchange="javascript:findDate();">
<asp:ListItem>Select Validity</asp:ListItem>
<asp:ListItem>Upto 7 Days</asp:ListItem>
<asp:ListItem>Upto 15 Days</asp:ListItem>
<asp:ListItem>Upto 30 Days</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>Enquiry Valid Upto:</td>
<td>
<label id="lblenddt" runat="server" ClientIDMode="static"></label>
</td>
</tr>
</table>
</asp:Content>
Please guide me where I am doing wrong?
Simply define a function findDate in javascript you have written $(function findDate() change it to function findDate() also you don't need to bind onchange again in javascript code you have already binded this in dropdown html onchange="javascript:findDate();"
function findDate() {
var selectvalid = $("#ddlvalid option:selected").text();
if (selectvalid == "Select Validity") {
alert("Please Select Validity");
}
else if (selectvalid == "Upto 7 Days") {
var today = new Date();
var tomorrow = new Date(today);
tomorrow.setDate(today.getDate() + 7);
var dd = tomorrow.getDate();
var mm = tomorrow.getMonth() + 1;
var y = tomorrow.getFullYear();
var output = tomorrow.getFullYear() + '/' + (mm < 10 ? '0' : '') + mm + '/' + (dd < 10 ? '0' : '') + dd;
document.getElementById("<%=lblenddt.ClientID%>").value = output;
}
else if (selectvalid == "Upto 15 Days") {
var today = new Date();
var tomorrow = new Date(today);
tomorrow.setDate(today.getDate() + 15);
var dd = tomorrow.getDate();
var mm = tomorrow.getMonth() + 1;
var y = tomorrow.getFullYear();
var output = tomorrow.getFullYear() + '/' + (mm < 10 ? '0' : '') + mm + '/' + (dd < 10 ? '0' : '') + dd;
document.getElementById("<%=lblenddt.ClientID%>").innerHTML = output;
}
else if (selectvalid == "Upto 30 Days") {
var today = new Date();
var tomorrow = new Date(today);
tomorrow.setDate(today.getDate() + 30);
var dd = tomorrow.getDate();
var mm = tomorrow.getMonth() + 1;
var y = tomorrow.getFullYear();
var output = tomorrow.getFullYear() + '/' + (mm < 10 ? '0' : '') + mm + '/' + (dd < 10 ? '0' : '') + dd;
document.getElementById("<%=lblenddt.ClientID%>").innerHTML = output;
}
}
window.onload = function() {
var d = new Date();
var month = d.getMonth() + 1;
var day = d.getDate();
var output = d.getFullYear() + '/' + (month < 10 ? '0' : '') + month + '/' + (day < 10 ? '0' : '') + day;
document.getElementById("<%=lblenqmdon.ClientID%>").innerHTML = output;
};
});
Edit
You have so set ClientIDMode="static" on dropdown and label to be found by javascript by exact id
I am having two Label controls and a dropdownlist on my web form. I am displaying current date in Label1 and I want to display Expiry Date in Label2 based on selection of dropdownlist. What I am trying to do is I want to display expiry date within Label2 on selecting data from dropdownlist i.e. if "Upto 7 Days" then 7Days will be added to current date and the new date will be displayed within Label2.
My aspx page-
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
$("#ddlvalid").change(function () {
var selectvalid = $("#ddlvalid option:selected").text();
if (selectvalid == "Select Validity") {
alert("Please Select Validity");
}
else if (selectvalid == "Upto 7 Days") {
var d = new Date();
var month = d.getMonth() + 1;
var day = d.getDate();
var output = d.getFullYear() + '/' + (month < 10 ? '0' : '') + month + '/' + (day < 10 ? '0' : '') + day;
var valdate = 7;
var expdate = d.setDate(day + valdate);
document.getElementById('lblenddt').innerHTML = expdate;
}
else if (selectvalid == "Upto 15 Days") {
var d = new Date();
var month = d.getMonth() + 1;
var day = d.getDate();
var output = d.getFullYear() + '/' + (month < 10 ? '0' : '') + month + '/' + (day < 10 ? '0' : '') + day;
var valdate = 15;
var expdate = d.setDate(day + valdate);
document.getElementById('lblenddt').innerHTML = expdate;
}
else if (selectvalid == "Upto 30 Days") {
var d = new Date();
var month = d.getMonth() + 1;
var day = d.getDate();
var output = d.getFullYear() + '/' + (month < 10 ? '0' : '') + month + '/' + (day < 10 ? '0' : '') + day;
var valdate = 30;
var expdate = d.setDate(day + valdate);
document.getElementById('lblenddt').innerHTML = expdate;
}
});
window.onload = function show() {
var d = new Date();
var month = d.getMonth() + 1;
var day = d.getDate();
var output = d.getFullYear() + '/' + (month < 10 ? '0' : '') + month + '/' + (day < 10 ? '0' : '') + day;
document.getElementById('lblenqmdon').innerHTML = output;
};
</script>
<body>
<div>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>Enquiry Made On:</td>
<td>
<label id="lblenqmdon" runat="server"></label>
</td>
</tr>
<tr>
<td>Enquiry Validity:</td>
<td><asp:DropDownList ID="ddlvalid" runat="server">
<asp:ListItem>Select Validity</asp:ListItem>
<asp:ListItem>Upto 7 Days</asp:ListItem>
<asp:ListItem>Upto 15 Days</asp:ListItem>
<asp:ListItem>Upto 30 Days</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>Enquiry Valid Upto:</td>
<td>
<label id="lblenddt" runat="server"></label>
</td>
</tr></table>
</div></body>
</html>
My Javascript code is not working. Please guide me where I am doing wrong?
Try This
<script type="text/javascript">
function findDate() {
var selectvalid = document.getElementById("<%=ddlvalid.ClientID %>").value;
if (selectvalid == "Select Validity") {
alert("Please Select Validity");
}
else if (selectvalid == "Upto 7 Days") {
var str = document.getElementById("<%=lblenqmdon.ClientID %>").innerHTML;
var parts = str.split('/');
var month = parts[0] && parseInt(parts[0], 10);
var day = parts[1] && parseInt(parts[1], 10);
var year = parts[2] && parseInt(parts[2], 10);
var duration = 7;
if (day <= 31 && day >= 1 && month <= 12 && month >= 1) {
var expiryDate = new Date(year, month - 1, day);
expiryDate.setDate(expiryDate.getDate() + duration);
var day = ('0' + expiryDate.getDate()).slice(-2);
var month = ('0' + (expiryDate.getMonth() + 1)).slice(-2);
var year = expiryDate.getFullYear();
document.getElementById("<%=lblenddt.ClientID %>").innerHTML = month + "/" + day + "/" + year;
}
}
else if (selectvalid == "Upto 15 Days") {
var str = document.getElementById("<%=lblenqmdon.ClientID %>").innerHTML;
var parts = str.split('/');
var month = parts[0] && parseInt(parts[0], 10);
var day = parts[1] && parseInt(parts[1], 10);
var year = parts[2] && parseInt(parts[2], 10);
var duration = 15;
if (day <= 31 && day >= 1 && month <= 12 && month >= 1) {
var expiryDate = new Date(year, month - 1, day);
expiryDate.setDate(expiryDate.getDate() + duration);
var day = ('0' + expiryDate.getDate()).slice(-2);
var month = ('0' + (expiryDate.getMonth() + 1)).slice(-2);
var year = expiryDate.getFullYear();
document.getElementById("<%=lblenddt.ClientID %>").innerHTML = month + "/" + day + "/" + year;
}
}
else if (selectvalid == "Upto 30 Days") {
var str = document.getElementById("<%=lblenqmdon.ClientID %>").innerHTML;
var parts = str.split('/');
var month = parts[0] && parseInt(parts[0], 10);
var day = parts[1] && parseInt(parts[1], 10);
var year = parts[2] && parseInt(parts[2], 10);
var duration = 30;
if (day <= 31 && day >= 1 && month <= 12 && month >= 1) {
var expiryDate = new Date(year, month - 1, day);
expiryDate.setDate(expiryDate.getDate() + duration);
var day = ('0' + expiryDate.getDate()).slice(-2);
var month = ('0' + (expiryDate.getMonth() + 1)).slice(-2);
var year = expiryDate.getFullYear();
document.getElementById("<%=lblenddt.ClientID %>").innerHTML = month + "/" + day + "/" + year;
}
}
}
</script>
<script type="text/javascript">
window.onload = function () {
getDate();
};
function getDate() {
var dt = new Date();
var day = dt.getDate();
var month = dt.getMonth() + 1;
var year = dt.getFullYear();
document.getElementById("<%=lblenqmdon.ClientID %>").innerHTML = month + "/" + day + "/" + year;
}
</script>
AND in Form
<asp:DropDownList ID="ddlvalid" runat="server" onchange="javascript:findDate();">
<asp:ListItem>Select Validity</asp:ListItem>
<asp:ListItem>Upto 7 Days</asp:ListItem>
<asp:ListItem>Upto 15 Days</asp:ListItem>
<asp:ListItem>Upto 30 Days</asp:ListItem>
</asp:DropDownList>
I managed to do the same like this-$(function () {
<script type="text/javascript">
$("#ddlvalid").change(function () {
var selectvalid = $("#ddlvalid option:selected").text();
if (selectvalid == "Select Validity") {
alert("Please Select Validity");
}
else if (selectvalid == "Upto 7 Days") {
var today = new Date();
var tomorrow = new Date(today);
tomorrow.setDate(today.getDate() + 7);
var dd = tomorrow.getDate();
var mm = tomorrow.getMonth() + 1;
var y = tomorrow.getFullYear();
var output = tomorrow.getFullYear() + '/' + (mm < 10 ? '0' : '') + mm + '/' + (dd < 10 ? '0' : '') + dd;
document.getElementById('lblenddt').innerHTML = output;
}
else if (selectvalid == "Upto 15 Days") {
var today = new Date();
var tomorrow = new Date(today);
tomorrow.setDate(today.getDate() + 15);
var dd = tomorrow.getDate();
var mm = tomorrow.getMonth() + 1;
var y = tomorrow.getFullYear();
var output = tomorrow.getFullYear() + '/' + (mm < 10 ? '0' : '') + mm + '/' + (dd < 10 ? '0' : '') + dd;
document.getElementById('lblenddt').innerHTML = output;
}
else if (selectvalid == "Upto 30 Days") {
var today = new Date();
var tomorrow = new Date(today);
tomorrow.setDate(today.getDate() + 30);
var dd = tomorrow.getDate();
var mm = tomorrow.getMonth() + 1;
var y = tomorrow.getFullYear();
var output = tomorrow.getFullYear() + '/' + (mm < 10 ? '0' : '') + mm + '/' + (dd < 10 ? '0' : '') + dd;
document.getElementById('lblenddt').innerHTML = output;
}
});
window.onload = function show() {
var d = new Date();
var month = d.getMonth() + 1;
var day = d.getDate();
var output = d.getFullYear() + '/' + (month < 10 ? '0' : '') + month + '/' + (day < 10 ? '0' : '') + day;
document.getElementById('lblenqmdon').innerHTML = output;
};
});
</script>
How do I format a date in Javascript to something e.g. 'yyyy-MM-dd HH:mm:ss z'?
This date.toString('yyyy-MM-dd HH:mm:ss z'); never work out for me :/
Any idea?
======
I solved my own which I rewrote like this:
var parseDate = function(date) {
var m = /^(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d) UTC$/.exec(date);
var tzOffset = new Date(+m[1], +m[2] - 1, +m[3], +m[4], +m[5], +m[6]).getTimezoneOffset();
return new Date(+m[1], +m[2] - 1, +m[3], +m[4], +m[5] - tzOffset, +m[6]);
}
var formatDateTime = function(data) {
var utcDate = parseDate(data);
var theMonth = utcDate.getMonth() + 1;
var myMonth = ((theMonth < 10) ? "0" : "") + theMonth.toString();
var theDate = utcDate.getDate();
var myDate = ((theDate < 10) ? "0" : "") + theDate.toString();
var theHour = utcDate.getHours();
var myHour = ((theHour < 10) ? "0" : "") + theHour.toString();
var theMinute = utcDate.getMinutes();
var myMinute = ((theMinute < 10) ? "0" : "") + theMinute.toString();
var theSecond = utcDate.getSeconds();
mySecond = ((theSecond < 10) ? "0" : "") + theSecond.toString();
var theTimezone = new Date().toString();
var myTimezone = theTimezone.indexOf('(') > -1 ?
theTimezone.match(/\([^\)]+\)/)[0].match(/[A-Z]/g).join('') :
theTimezone.match(/[A-Z]{3,4}/)[0];
if (myTimezone == "GMT" && /(GMT\W*\d{4})/.test(theTimezone)) {
myTimezone = RegExp.$1;
}
if (myTimezone == "UTC" && /(UTC\W*\d{4})/.test(theTimezone)) {
myTimezone = RegExp.$1;
}
var dateString = utcDate.getFullYear() + "-" +
myMonth + "-" +
myDate + " " +
myHour + ":" +
myMinute + ":" +
mySecond + " " +
myTimezone;
return dateString;
}
and I get: 2012-11-15 22:08:08 MPST :) PERFECT!
function formatDate(dateObject) //pass date object
{
return (dateObject.getFullYear() + "-" + (dateObject.getMonth() + 1)) + "-" + dateObject.getDate() ;
}
Use this lib to make your life much easier:
var formattedDate = new Date().format('yyyy-MM-dd h:mm:ss');
document.getElementById("time").innerHTML= formattedDate;
DEMO
Basically, we have three methods and you have to combine the strings for yourself:
getDate(): Returns the date
getMonth(): Returns the month
getFullYear(): Returns the year
Example:
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1; //Months are zero based
var curr_year = d.getFullYear();
document.write(curr_date + "-" + curr_month + "-" + curr_year); </script>
for more details look at 10 steps to format date and time and also check this