Need Help With Date Function

Need Help With Date Function
0.0 0

#1

Hi,

I am creating a form where i am taking input from user birth details , including time, now i want to create a date object like -

var d = new Date("2015-03-25T12:00:00Z");

I have been able to achieve every thing in terms of getting hour , weekday, etc but cannot seem to add “T12:00:00Z” to achieve my time aspect when i try and add it gives an error , i am getting my hour and minute via -

var testHour = document.setHoursMinutes.hour.value;  //// here setHoursMinutes are dropdown names etc
var testMinute = document.setHoursMinutes.minute.value;

Below is my function code kindly guide how i can achieve this … Thanks


function getDateInfo() {
   var a = document.selectDate.month.value;  //// Month DropDown
   var c = document.selectDate.year.value;   ///// year text box
   var b = document.selectDate.day.value;    ////Day Drop Dwn
   var testHour = document.setHoursMinutes.hour.value;    /////Hour text box
   var testMinute = document.setHoursMinutes.minute.value;   //// Minute text box
    document.displayInfo.month.value=document.selectDate.month.value;  //// Month Drop Down
		 
    document.displayInfo.day.value=document.selectDate.day.value;		
    var daysArr = ['Sunday','Moday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
    var dateStr = c + "-" + a + "-" + b;  /////Here is my issue, i cannot seem to add T12:00:00Z where 12 is my hour and 00 my minute value
   
    var newDate = new Date(dateStr);  ///  This date object i want to be like **new Date("2015-03-25T12:00:00Z");**

    var dayNo = newDate.getDay();

    var disDay = daysArr[dayNo];

   document.displayInfo.weekDay.value=disDay;	

   document.displayInfo.fullYear.value=document.selectDate.year.value;	

  
    
    
}

#2

Verbose but intent is clear:

function createUserBirthDate(y,m,d,hr,min) {
  var userBirth = new Date();
  userBirth.setFullYear(y);
  userBirth.setMonth(m);
  userBirth.setDate(d);
  userBirth.setHours(hr);
  userBirth.setMinute(min);

  return userBirth;
}

The Date object has get and set functions that modify it: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date


#3

Hi @DanCouper , Thanks as per your suggestion i tried to set the values and then get them and pass them on but i keep getting invalid date error … below is my code kindly guide where i am wrong —



function getDateInfo() {
   
    var daysArr = ['Sunday','Moday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
    
    var userBirth = new Date();
    var y = document.selectDate.year.value;
    userBirth.setFullYear(y);
    var m = document.selectDate.month.value;
    userBirth.setMonth(m);
    var date1 = parseInt(document.selectDate.day.value);
   
    userBirth.setDate(date1);
    
    var hr = parseInt(document.setHoursMinutes.hour.value);
    userBirth.setHours(hr);
     
    var min = parseInt(document.setHoursMinutes.minute.value);
    userBirth.setMinutes(min);
  
    var getDay = userBirth.getDay();
    var disWeekDay = daysArr[getDay];
    var disYear = userBirth.getFullYear();
    var disMonth = userBirth.getMonth();
    var disDay = userBirth.getDay();
    
    
    /////-----Dispaly values
    
    document.displayInfo.month.value=disMonth; 
	document.displayInfo.day.value=disDay;
	document.displayInfo.weekDay.value=disWeekDay;
	document.displayInfo.fullYear.value=disYear;
    
   
}

#4

Do you have this up somewhere like CodePen?


#5

@DanCouper - Thanks , i was making a mistake of trying to enter August as string rather then month number as the argument needs to be number , i created a switch and it solved the problem … Thank you so much for letting me know about these methods , i can now code much better with date to some extend …

Below is updated code …



function getDateInfo() {
   
    var daysArr = ['Sunday','Moday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
    
    var userBirth = new Date();
    var y = document.selectDate.year.value;
    userBirth.setFullYear(y);
    
    var m = document.selectDate.month.value;
    switch (m) {
        case "January" :
          userBirth.setMonth(0);
            break;
            
            case "February" :
          userBirth.setMonth(1);
            break;
            
            case "March" :
          userBirth.setMonth(2);
            break;
            
            case "April" :
          userBirth.setMonth(3);
            break;
            
            case "May" :
          userBirth.setMonth(4);
            break;
            
            case "June" :
          userBirth.setMonth(5);
            break;
            
            case "July" :
          userBirth.setMonth(6);
            break;
            
            case "August" :
          userBirth.setMonth(7);
            break;
            
            case "September" :
          userBirth.setMonth(8);
            break;
            
            case "October" :
          userBirth.setMonth(9);
            break;
            
            case "November" :
          userBirth.setMonth(10);
            break;
            
            case "December" :
          userBirth.setMonth(11);
            break;
            
            
    }
    
  
    var date1 = parseInt(document.selectDate.day.value);
   
    userBirth.setDate(date1);
    
    var hr = parseInt(document.setHoursMinutes.hour.value);
    userBirth.setHours(hr);
     
    var min = parseInt(document.setHoursMinutes.minute.value);
    userBirth.setMinutes(min);
  
    var getDay = userBirth.getDay();
    var disWeekDay = daysArr[getDay];
    var disYear = userBirth.getFullYear();
    var disMonth = m;
    var disDay = userBirth.getDay();
    
    alert(userBirth);
    /////-----Dispaly values
    
    document.displayInfo.month.value=disMonth; 
	document.displayInfo.day.value=disDay;
	document.displayInfo.weekDay.value=disWeekDay;
	document.displayInfo.fullYear.value=disYear;
    
   
}

#6

:+1: You can shrink that down by

['January','February',...rest of months].indexOf(month);

Rather than the switch


#7

you may want to look at various html date and time inputs

<input id=dob type=datetime-local>

#8

Date inputs flat out don’t work on Firefox or IE mind


#9

Dealing with dates in JS can be a pain.

depending on what you are doing, a utility library might save some time. These two are both excellent and worth looking at.

https://momentjs.com/