Reverse the order of an auto date format - javascript

The function I am using formats inserts '-' at certain points to auto format the date field. Right now it prints in the format 'mm-dd-yyyy', I am trying to get it to print 'yyy-mm-dd'.
<input type=textbox id=date onkeyup="dtval(this,event)" />
the js
function dtval(d,e) {
var pK = e ? e.which : window.event.keyCode;
if (pK == 8) {d.value = substr(0,d.value.length-1); return;}
var dt = d.value;
var da = dt.split('-');
for (var a = 0; a < da.length; a++) {if (da[a] != +da[a]) da[a] = da[a].substr(0,da[a].length-1);}
if (da[0] > 31) {da[1] = da[0].substr(da[0].length-1,1);da[0] = '0'+da[0].substr(0,da[0].length-1);}
if (da[1] > 12) {da[2] = da[1].substr(da[1].length-1,1);da[1] = '0'+da[1].substr(0,da[1].length-1);}
if (da[2] > 9999) da[1] = da[2].substr(0,da[2].length-1);
dt = da.join('-');
if (dt.length == 2 || dt.length == 5) dt += '-';
d.value = dt;
}

I think you'd benefit from the Date class.
var date1 = new Date('02-04-2016');
//Thu Feb 04 2016 00:00:00 GMT-0500 (Eastern Standard Time)
var date2 = date1.getFullYear() + '-'
+ ((date1.getMonth() + 1) > 9? date1.getMonth() + 1: '0' + (date1.getMonth() + 1)) + '-'
+ (date1.getDate() > 9? date1.getDate(): '0' + date1.getDate());
//"2016-02-04"

Here it is for YYYY-MM-DD:
function dtval(d,e) {
var pK = e ? e.which : window.event.keyCode;
if (pK == 8) {d.value = d.value.substr(0,d.value.length-1); return;}
var dt = d.value;
var da = dt.split('-');
for (var a = 0; a < da.length; a++) {
if (da[a] != +da[a]) da[a] = da[a].substr(0,da[a].length-1);
}
if (da[0] > 9999) {da[1] = da[0].substr(da[0].length-1,1);da[0] = '0'+da[0].substr(0,da[0].length-1);}
if (da[1] > 12) {da[2] = da[1].substr(da[1].length-1,1);da[1] = '0'+da[1].substr(0,da[1].length-1);}
if (da[2] > 31) da[1] = da[2].substr(0,da[2].length-1);
dt = da.join('-');
if (dt.length == 4 || dt.length == 7) dt += '-';
d.value = dt;
}
<input type=textbox id=date onkeyup="dtval(this,event)" />
BTW, you had this line:
if (pK == 8) {d.value = substr(0,d.value.length-1); return;}
There's no substr global function, it's a function of String:
if (pK == 8) {d.value = d.value.substr(0,d.value.length-1); return;}

Related

Can't Find Cause and Solution to Javascript Error

When I run my code, there's an error saying: Cannot set property 'src' of null
The error is in this for loop:
for (i=1; i<=48; i++) {
document.getElementById("hour" + i*6 + "map").src = mapAddresses[i-1];
}//end for
Here is all of the code leading up to that point:
<script>
//Create Canvases and Images
var canvas;
var img;
var index;
for (var i = 1; i <= 40; i++) {
index = 6 * i;
canvas = document.createElement("canvas");
canvas.id = "hour" + index + "canvas";
canvas.width = 1024;
canvas.height = 764;
img = document.createElement("img");
img.id = "hour" + index + "map";
img.src = " ";
canvas.appendChild(img);
document.body.appendChild(canvas);
}//end for
//Time Variables
var currentYear = new Date().getFullYear();
var currentMonth = new Date().getMonth() + 1;
var currentDay = new Date().getDate();
var currentHour = new Date().getHours();
var currentRun;
//Formatting Time Variables for URLs
if (currentMonth < 10) {
currentMonth = "0" + currentMonth;
}//end if
if (currentDay < 10) {
currentDay = "0" + currentDay;
}//end if
//Finding Latest Model Run
if (currentHour >= 0 && currentHour < 6) {
currentRun = "00";
}//end if
if (currentHour >= 6 && currentHour < 12) {
currentRun = "06";
}//end if
if (currentHour >= 12 && currentHour < 18) {
currentRun = "12";
}//end if
if (currentHour >= 18 && currentHour < 24) {
currentRun = "18";
}//end if
var currentRun = currentRun;
//Creating URLs
var mapAddresses = [];
for (i=6; i<=240; i=i+6) {
mapAddressFor = "http://www.tropicaltidbits.com/analysis/models/gfs/" + currentYear + currentMonth + currentDay + currentRun + "/gfs_mslp_pcpn_neus_" + i/6 + ".png";
mapAddresses.push(mapAddressFor);
}//end for
//Insert Images to Document
for (i=1; i<=48; i++) {
document.getElementById("hour" + i*6 + "map").src = mapAddresses[i-1];
}//end for
How can I get rid of this error? I can't figure our why there's an error, and how can I fix it?
You create 40 elements:
for (var i = 1; i <= 40; i++) {
// ...
img.id = "hour" + index + "map";
// ...
}//end for
and try to set 48
for (i=1; i<=48; i++) {
document.getElementById("hour" + i*6 + "map").src = mapAddresses[i-1];
}//end for

show appointments/ dates of today with JavaScript function

I want to show the actual appointments of the day in my HTML. I would like to implement it with a JavaScript where I get the name of the appointment and the date out of an excel table.
I already wrote something but it doesn't work and not in the way I want it.
Code:
function appointment() {
var event = [];
var temp = [];
event[0] = ["17.12.2015", "test1"];
event[1] = ["11.12.2015", "TestToday"];
var datum = new Date();
var today = today.getDate();
var month = today.getMonth() + 1;
for (i = 0; i < event.length; i++) {
if (event[i]) {
if (event[i][0] == today && event[i][0] == month) {
event[i][0] = temp[i];
}
}
else {
break;
}
}
if (temp.length == 0) {
document.write("Today is nothing to do");
}
else {
var x2 = "Today " + ((temp.length == 1) ? "is following event: " : "are following events: ");
for (i = 0; i < temp.length; i++) {
x2 += ((temp[i] > 0) ? ((temp[i] == (temp.length - 1)) ? " and " : ", ") : " ") + temp[event[1]][3] + "(" + temp[event[i]][2] + ")";
}
document.write(x2 + " appointment");
}
}
Question: How can I make it work? How can I read the appointment toppic and date out of a excel table?
Change your code to
var datum = new Date().setHours(0,0,0,0);
//you don't need today and month
if (event[i]) {
eventDate = new Date(event[i][0].split(".").reverse()).getTime();
if (datum === eventDate) {
temp.push(event[i][1]); //there was a problem here as well
}
}
else {
break;
}
jsFiddle
Issue is in your for loop. To compare today's date should be in dd.mm.yy format as it is in array:
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10)
dd='0'+dd;
if(mm<10)
mm='0'+mm;
var today = dd+'.'+mm+'.'+yyyy;
for (i = 0; i < event.length; i++) {
if (event[i]) {
if (event[i][0] == today) {
temp[i] = event[i][0];
}
}
// Don't break your for loop
}
Working Fiddle

Date format Issue using javascript

In the below code i have a textbox my textbox should return a date format as yyyy-mm-dd.But it returns dd-mm-yyyy.Pls help me to rectify the issue.
js:
function Reportdate(txt, keyCode) {
alert(txt);
if (keyCode == 8) { txt.value = substr(0, txt.value.length - 1); return; }
var dt = txt.value;
var da = dt.split('-');
for (var a = 0; a < da.length; a++) { if (da[a] != +da[a]) da[a] = da[a].substr(0, da[a].length - 1); }
if (da[0] > 9999) da[1] = da[0].substr(0, da[0].length - 1);
if (da[1] > 12) { da[2] = da[1].substr(da[1].length - 1, 1); da[1] = '0' + da[1].substr(0, da[1].length - 1); }
if (da[2] > 31) { da[1] = da[2].substr(da[2].length - 1, 1); da[2] = '0' + da[2].substr(0, da[2].length - 1); }
dt = da.join('-');
if (dt.length == 2 || dt.length == 5) dt += '-';
txt.value = dt;
}
ASP.net
<asp:TextBox ID="txtFromDate" onkeydown = "return Reportdate(this, event.keyCode)" runat="server"></asp:TextBox>

How to disable specific dates in Magento Order Delivery Date extensions calendar

I want to disable specific dates in Magento Connect Order Delivery Date Calendar and tried many ways. But could not be successful. Below is my code. It says "TypeError: $.inArray is not a function" when click on the calendar icon.
disableFunc : function(date)
{
var day_off_array = dayoff.split(",") ;
currentTime = new Date();
var d1=currentTime.getDate();
var m1=currentTime.getMonth();
var y1=currentTime.getFullYear();
m1=m1+1;
var counter = 1;
var disabledDays =
["10-20-2013", "10-21-2013", "11-15-2013", "11-17-2013"];
var m = date.getMonth(), d = date.getDate(), y = date.getFullYear();
for (i = 0; i < disabledDays.length; i++) {
if($.inArray((m+1) + '-' + d + '-' + y,disabledDays) != -1) {
return [false];
}
}
return [true];
if(y < y1)
{
return true;
}
else if(m1 > m && y==y1)
{
return true;
}
}
and this is the original code.
disableFunc : function(date)
{
var y = calendar.date.getFullYear();
var m = calendar.date.getMonth();
var d = calendar.date.getDate();
var day_off_array = dayoff.split(",") ;
//document.write(day_off_array);
currentTime = new Date();
var d1=currentTime.getDate();
var m1=currentTime.getMonth();
var y1=currentTime.getFullYear();
var counter = 1;
for (var i=0; i<day_off_array.length;i++){
if (day_off_array[i]>=0 && day_off_array[i]!=''){
if (date.getDay()==day_off_array[i]){
if (date.getDate()==currentTime.getDate()){
test_flag=true;
}
return true;
}
}
}
if(y < y1)
{
return true;
}
else if(m1 > m && y==y1)
{
return true;
}
}
Any helps would be appreciated.
It's too late to answer the question, but this may help someone like me!
var disabledDays = ["10-20-2014", "10-21-2014", "11-15-2014", "11-17-2014"];
var m = date.getMonth(), d = date.getDate(), y = date.getFullYear();
for (i = 0; i < disabledDays.length; i++) {
var dd = (m+1) + '-' + d + '-' + y;
if(disabledDays.indexOf(dd) != -1) {
return true;
}
}

JavaScript to validate date

Below is the function to validate date. The should be between Today - 15 and Today. Can some one refactor this code.
phpdatetoday is a string in the form 2010,Dec,3
function validate(page, phpdatetoday)
{
var i = 0;
var fields = new Array();
var fieldname = new Array();
var day = document.getElementById('date_of_inspection_day').value;
var month = document.getElementById('date_of_inspection_month').value;
var year = document.getElementById('date_of_inspection_year').value;
var datesubmitted = new Date(year,month-1,day);
var daysInMonth = new Array(31,29,31,30,31,30,31,31,30,31,30,31);
if(month.length<1 )
{
alert("Please enter a valid month");
return false;
}
if(year.length != 4 )
{
alert("Please enter a valid year");
return false;
}
if (day.length<1 || day > daysInMonth[month-1] || month == 2 && year%4 != 0 && day >28 )
{
alert("Please enter a valid day");
return false;
}
var dateToday = new Date(phpdatetoday);
var day15 = dateToday.getDate()-15; // 15 days old
var month15 = dateToday.getMonth();
var year15 = dateToday.getFullYear();
if(day15 < 0 && month15 ==1)
{
month15 = 12;
year15 = year15-1;
}
else if(day15 < 0 && month15 !=1)
{
month15 = month15-1;
}
day15 = daysInMonth[month15-1] + day15;
var date15DayOld = new Date(year15,month15,day15);
if(date15DayOld > datesubmitted )
{
alert("Your date is older than 15 days");
}
else if(datetoday < datesubmitted )
{
alert("invalid Date");
}
}
function validate(phpdatetoday, withinDays) {
var inputDateInMillis = Date.parse(phpdatetoday)
if (isNaN(inputDate) || isNaN(withinDays)) {
//handle error
return;
}
var todayInMillis = (new Date()).setHours(0,0,0,0);
return todayInMillis - inputDateInMillis < (withinDays * 86400000 /*1000ms*60s*60m*24h*/);
}
Date.setHours() will set the hours/minutes/seconds to zero and return milliseconds since 1 Jan 1970 UTC.
Date.parse() will return the parsed date otherwise if it cannot do it then it will return NaN. You can use isNan() to determine whether a variable's value is a number or not. If 'inputDate' is NaN then you can alert the user that the input date was invalid.
eval(function(p,a,c,k,e,d){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--){d[e(c)]=k[c]||e(c)}k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}('C K(L,w){3 i=0;3 H=b q();3 I=b q();3 c=k.t(\'J\').s;3 9=k.t(\'G\').s;3 d=k.t(\'A\').s;3 u=b j(d,9-1,c);3 l=b q(7,E,7,g,7,g,7,7,g,7,g,7);6(9.n<1){e("v r a p 9");m o}6(d.n!=4){e("v r a p d");m o}6(c.n<1||c>l[9-1]||9==2&&d%4!=0&&c>R){e("v r a p c");m o}3 f=b j(w);3 8=f.N()-y;3 5=f.Q();3 h=f.P();6(8<0&&5==1){5=O;h=h-1}x 6(8<0&&5!=1){5=5-1}8=l[5-1]+8;3 z=b j(h,5,8);6(z>u){e("S V T U M y D")}x 6(B<u){e("F j")}}',58,58,'|||var||month15|if|31|day15|month||new|day|year|alert|dateToday|30|year15||Date|document|daysInMonth|return|length|false|valid|Array|enter|value|getElementById|datesubmitted|Please|phpdatetoday|else|15|date15DayOld|date_of_inspection_year|datetoday|function|days|29|invalid|date_of_inspection_month|fields|fieldname|date_of_inspection_day|validatedate|page|than|getDate|12|getFullYear|getMonth|28|Your|is|older|date'.split('|'),0,{}))
function validate(page, phpdatetoday){return validatedate(page, phpdatetoday);}
refactored!(?)

Categories