Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    How to create Date object and send to Date Constructor

    I have to Create a Date object for the user's date of birth. To get the info I have used this code:

    Code:
     <script type="text/javascript">
    
    
      var months = 'janfebmaraprmayjunjulaugsepoctnovdec';
      var Pl = 3.14159265358979323846; 
      var milliseconds = 1000*60*60*24;
      var interllectualCycle = 33;
      var emotionalCycle = 28;
      var physicalCycle = 23;       
    
    
      var birthYear = prompt( 'Enter year of birth as a 4 digit integer' );
      alert('Your name is '+year);
      var birthMonth = prompt( 'Enter the name of the month of birth' );
    
      var pos = months.indexOf( birthMonth.substring( 0, 3 ).toLowerCase() );
    
    
    
     alert( 'Month number: ' + ( 1 + pos / 3 ) );
    
      var birthDay = prompt( 'Enter day of birth as an integer ');
    but now I am stuck on how to create Date object for user's date of birth? I have to send the Date constructor the year, calculated month number and day.

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,474
    Thanks
    13
    Thanked 361 Times in 357 Posts
    you create a Date object via the Date() constructor by passing to it year, month and day (more info: https://developer.mozilla.org/en-US/...l_Objects/Date)

    PS. there is already a representation of pi is JavaScript: Math.PI
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Why do you prompt for month name instead of just month number? Typing text is more error prone compared to numbers.

    If you prompt for numeric birth month, then the date of birth could be easily created.
    Code:
    var dob = new Date(birthYear, birthMonth - 1, birthDay);
    alert(dob);
    Of course, you have to validate the inputs to ensure the valid values are entered.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,314
    Thanks
    203
    Thanked 2,565 Times in 2,543 Posts
    var birthYear = prompt( 'Enter year of birth as a 4 digit integer' );
    alert('Your name is '+year);

    year is undefined.

    What if the user enters -12345?

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    New Coder
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by glenngv View Post
    Why do you prompt for month name instead of just month number? Typing text is more error prone compared to numbers.

    If you prompt for numeric birth month, then the date of birth could be easily created.
    Code:
    var dob = new Date(birthYear, birthMonth - 1, birthDay);
    alert(dob);
    Of course, you have to validate the inputs to ensure the valid values are entered.
    Thanks Glenn,

    It would be alot easier if I could as for the month by number but I have to as for month in text and then convert to the number.

    What I came up with was



    var yearString = 'birthyear';
    var monthNumber = '';
    var dayString = 'birthDay';

    //the date object is created like so:

    var myBirthday = new Date(yearString, monthNumber, dayString);
    alert (myBirthday)

    //now a date object is contained in the variable called myBirthday

    but not sure how to take the pos and put that into the monthNumber

  • #6
    New Coder
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by glenngv View Post
    Why do you prompt for month name instead of just month number? Typing text is more error prone compared to numbers.

    If you prompt for numeric birth month, then the date of birth could be easily created.
    Code:
    var dob = new Date(birthYear, birthMonth - 1, birthDay);
    alert(dob);
    Of course, you have to validate the inputs to ensure the valid values are entered.
    Hi again Glenn,

    This is what I came up with and it seems to work:

    Code:
    <script>
      var months = 'janfebmaraprmayjunjulaugsepoctnovdec';
      var Pl = 3.14159265358979323846; 
      var milliseconds = 1000*60*60*24;
      var interllectualCycle = 33;
      var emotionalCycle = 28;
      var physicalCycle = 23;       
    
    
      var birthYear =  prompt( 'Enter year of birth as a 4 digit integer');
     //alert('Your year of birth is '+ birthYear);
      var birthMonth = prompt( 'Enter the name of the month of birth' );
    
      var pos = months.indexOf( birthMonth.substring( 0, 3 ).toLowerCase() );
    
     //alert( 'Month number: ' + ( 1 + pos / 3 ) );
    
      var birthDay = prompt( 'Enter day of birth as an integer ');
      
      
      var yearString = birthYear;
      var monthNumber = pos;  
      var dayString = birthDay;
    
    //the date object is created like so:
    
    var myBirthday = new Date(yearString - 1, monthNumber, dayString);
    alert ('myBirthday' + myBirthday);
    
    //now a date object is contained in the variable called myBirthday
      
      </script>
    Does it look the way it should?

    Thanks

    Sonya

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,314
    Thanks
    203
    Thanked 2,565 Times in 2,543 Posts
    Quote Originally Posted by sonya1m View Post
    Hi again Glenn,

    This is what I came up with and it seems to work:

    Code:
    <script>
      var months = 'janfebmaraprmayjunjulaugsepoctnovdec';
      var Pl = 3.14159265358979323846; 
      var milliseconds = 1000*60*60*24;
      var interllectualCycle = 33;
      var emotionalCycle = 28;
      var physicalCycle = 23;       
    
    
      var birthYear =  prompt( 'Enter year of birth as a 4 digit integer');
     //alert('Your year of birth is '+ birthYear);
      var birthMonth = prompt( 'Enter the name of the month of birth' );
    
      var pos = months.indexOf( birthMonth.substring( 0, 3 ).toLowerCase() );
    
     //alert( 'Month number: ' + ( 1 + pos / 3 ) );
    
      var birthDay = prompt( 'Enter day of birth as an integer ');
      
      
      var yearString = birthYear;
      var monthNumber = pos;  
      var dayString = birthDay;
    
    //the date object is created like so:
    
    var myBirthday = new Date(yearString - 1, monthNumber, dayString);
    alert ('myBirthday' + myBirthday);
    
    //now a date object is contained in the variable called myBirthday
      
      </script>
    Does it look the way it should?

    Thanks

    Sonya
    Simple testing ought to reveal that it does not work.

    When I enter my birthday the year and the month are both displayed wrongly. Why do you subtract 1 from the year?

    var monthNumber = pos; // is simply silly - December comes out as month 33!

    Prompts are long obsolete. You should eliminate them. Have you not noticed that the initial value is displayed as "undefined"?

    PI is already defined in Javascript as Math.PI.

    Why do you ignore glenngv's advice:-

    Why do you prompt for month name instead of just month number? Typing text is more error prone compared to numbers. If you prompt for numeric birth month, then the date of birth could be easily created.
    Thre is no point in coming to this forum for expert help and then disregarding the advice offered.
    Last edited by Philip M; 03-20-2013 at 03:52 PM. Reason: Noticed typo

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #8
    New Coder
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I would really love to prompt for the month in number, but my assignment says I have to ask for it in text then convert to number.

    Sorry if it does seem like I am not taking glenns advice, but as I previously explained I can't change my prompt.

    Sonya

  • #9
    New Coder
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I should have explained that this is an assignment I have been given by my uni professor and I need to do as it is set out for me.

    I have tested it and when i -1 from year, it comes up right. (not sure why I need to do that)

    Using Prompt, is how I am being shown at uni, it could possibly be that when I have learn this first stage, I may be shown a different way.

    Thank you for your comments I will bring these things to him and find out why I am learning this way.

    Sonya

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,314
    Thanks
    203
    Thanked 2,565 Times in 2,543 Posts
    Quote Originally Posted by sonya1m View Post
    I have tested it and when i -1 from year, it comes up right. (not sure why I need to do that)
    Not for me it doesn't. And it cannot possibly be right for you either.
    It makes no sense at all to deduct one from the birthyear. Have you tried to copy some script which calculates a person's age? Then of course an anniversary is reached only on the birthday.
    So someone born in 2000 could be either 12 or 13 today.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #11
    New Coder
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hi Philip,

    Thank you , i was only putting in my birthday and it was working for that. In future I will know to check other dates ect. So I now have that part of it working.

    Thank you for you help

    Sonya


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •