Inserting today's date using JavaScript

Discussion in 'ASP .Net' started by =?Utf-8?B?UmljaGFyZA==?=, Oct 11, 2005.

  1. I have a textbox and an HtmlInputButton control on a Webform for an Intranet
    app. When the user clicks the button, it is supposed to add today's date to
    the textbox using JavaScript on the client side.

    However, it is adding last month's date to the textbox, not today's date. I
    checked the regional settings and date/time section of both the client
    machine and the web server. Both are set correctly for CA and daylight
    savings time, and both show the correct date/time on the Taskbar.

    Here is the JavaScript I'm using that I found Googling:
    function addDate(){
    TheDate = new Date();
    var theMonth = TheDate.getMonth();
    var theDay = TheDate.getDate();
    var theYear = TheDate.getFullYear();

    if (document.getElementById('txtFollowUpNeeds').value == ''){
    document.getElementById('txtFollowUpNeeds').value = theMonth + '/' + theDay
    + '/' + theYear + ': ';
    }
    else {
    document.getElementById('txtFollowUpNeeds').value += '\r\n' + theMonth + '/'
    + theDay + '/' + theYear + ': ';
    }
    }

    And here is the line in my Page_Load event:
    btnAddDate.Attributes.Add("onClick", "addDate();")

    Any ideas on how to make this work?

    Thanks in advance!
     
    =?Utf-8?B?UmljaGFyZA==?=, Oct 11, 2005
    #1
    1. Advertising

  2. =?Utf-8?B?UmljaGFyZA==?=

    Bruce Barker Guest

    getMonth() is zero based, so you need to add one to it.

    -- bruce (sqlwork.com)


    "Richard" <> wrote in message
    news:...
    >I have a textbox and an HtmlInputButton control on a Webform for an
    >Intranet
    > app. When the user clicks the button, it is supposed to add today's date
    > to
    > the textbox using JavaScript on the client side.
    >
    > However, it is adding last month's date to the textbox, not today's date.
    > I
    > checked the regional settings and date/time section of both the client
    > machine and the web server. Both are set correctly for CA and daylight
    > savings time, and both show the correct date/time on the Taskbar.
    >
    > Here is the JavaScript I'm using that I found Googling:
    > function addDate(){
    > TheDate = new Date();
    > var theMonth = TheDate.getMonth();
    > var theDay = TheDate.getDate();
    > var theYear = TheDate.getFullYear();
    >
    > if (document.getElementById('txtFollowUpNeeds').value == ''){
    > document.getElementById('txtFollowUpNeeds').value = theMonth + '/' +
    > theDay
    > + '/' + theYear + ': ';
    > }
    > else {
    > document.getElementById('txtFollowUpNeeds').value += '\r\n' + theMonth +
    > '/'
    > + theDay + '/' + theYear + ': ';
    > }
    > }
    >
    > And here is the line in my Page_Load event:
    > btnAddDate.Attributes.Add("onClick", "addDate();")
    >
    > Any ideas on how to make this work?
    >
    > Thanks in advance!
     
    Bruce Barker, Oct 11, 2005
    #2
    1. Advertising

  3. Thanks Bruce! That did it!

    "Bruce Barker" wrote:

    > getMonth() is zero based, so you need to add one to it.
    >
    > -- bruce (sqlwork.com)
    >
    >
    > "Richard" <> wrote in message
    > news:...
    > >I have a textbox and an HtmlInputButton control on a Webform for an
    > >Intranet
    > > app. When the user clicks the button, it is supposed to add today's date
    > > to
    > > the textbox using JavaScript on the client side.
    > >
    > > However, it is adding last month's date to the textbox, not today's date.
    > > I
    > > checked the regional settings and date/time section of both the client
    > > machine and the web server. Both are set correctly for CA and daylight
    > > savings time, and both show the correct date/time on the Taskbar.
    > >
    > > Here is the JavaScript I'm using that I found Googling:
    > > function addDate(){
    > > TheDate = new Date();
    > > var theMonth = TheDate.getMonth();
    > > var theDay = TheDate.getDate();
    > > var theYear = TheDate.getFullYear();
    > >
    > > if (document.getElementById('txtFollowUpNeeds').value == ''){
    > > document.getElementById('txtFollowUpNeeds').value = theMonth + '/' +
    > > theDay
    > > + '/' + theYear + ': ';
    > > }
    > > else {
    > > document.getElementById('txtFollowUpNeeds').value += '\r\n' + theMonth +
    > > '/'
    > > + theDay + '/' + theYear + ': ';
    > > }
    > > }
    > >
    > > And here is the line in my Page_Load event:
    > > btnAddDate.Attributes.Add("onClick", "addDate();")
    > >
    > > Any ideas on how to make this work?
    > >
    > > Thanks in advance!

    >
    >
    >
     
    =?Utf-8?B?UmljaGFyZA==?=, Oct 11, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TB
    Replies:
    0
    Views:
    462
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,295
    Michael Borgwardt
    May 30, 2004
  3. Phil Powell
    Replies:
    2
    Views:
    156
    Julian Roberts
    Feb 17, 2004
  4. Vinnie Davidson

    ASP Date: get records with date = today (SQL Server)

    Vinnie Davidson, Aug 13, 2005, in forum: ASP General
    Replies:
    6
    Views:
    335
    Aaron Bertrand [SQL Server MVP]
    Aug 15, 2005
  5. joeyej
    Replies:
    7
    Views:
    157
    Randy Webb
    Apr 6, 2006
Loading...

Share This Page