How to pass datetime variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • James

    How to pass datetime variable

    Hello,

    I am a beginner in C# programming and I just want to know how we can
    pass a datetime variable in a method. can anyone please help me
    regarding this.

    Thanks,
    James.
  • =?iso-8859-1?B?S2VyZW0gR/xtcvxrY/w=?=

    #2
    Re: How to pass datetime variable

    Hi James,

    welcome to the great world of programming!

    Here is a example:

    public void PrintDateAndTim e(System.DateTi me DateAndTimeToPr int)
    {
    System.Console. WriteLine("Actu al Date and Time: {0}",
    DateAndTimeToPr int.ToString()) ;
    }

    you invoke it that way:

    PrintDateAndTim e(System.DateTi me.Now);

    Asuming that you are working on a Console!

    You should read the basics about functions/members/methods:
    [Methods (C# Programming Guide)]
    A method in C# is a code block that contains a series of statements. A program runs the statements by calling the method and specifying arguments.


    I higly recommend you to read the C# language reference, because
    it is a great starting point for beginners:

    [C# Programming Guide]


    If you have more questions, feel free to ask them here:

    Enjoy,...!

    Regards

    Kerem

    --
    -----------------------
    Beste Grüsse / Best regards / Votre bien devoue
    Kerem Gümrükcü
    Latest Project: http://www.codeplex.com/restarts
    Latest Open-Source Projects: http://entwicklung.junetz.de
    -----------------------
    "This reply is provided as is, without warranty express or implied."

    Comment

    • Alberto Poblacion

      #3
      Re: How to pass datetime variable

      "James" <bondrulesit200 8@gmail.comwrot e in message
      news:329a49e8-f46f-409c-aaea-47c7ba46c6c5@x3 5g2000hsb.googl egroups.com...
      I am a beginner in C# programming and I just want to know how we can
      pass a datetime variable in a method. can anyone please help me
      regarding this.
      I assume that you already know how to declare the method and that your
      problem is that you don't know how to insert into your code a given date,
      such as "September 28th, 2008". One of the ways to do it is by means of one
      of the constructors of the DateTime class, which takes year, month and day
      as parameters:

      DateTime dateToPass = new DateTime(2008,9 ,28);
      MyMethod(dateTo Pass);
      .....


      private void MyMethod(DateTi me argument)
      {
      //Here 'argument' contains the date that you passed.
      }


      Comment

      • =?Utf-8?B?Um9iIEphcnJldHQ=?=

        #4
        Re: How to pass datetime variable

        Do you know how to pass date arguments in C#?

        VB Example

        Public Sub Test (datTest As Date)
        MsgBox(datTest. ToString())
        End Sub

        Public Sub TestCall()
        Me.Test(#1/1/2001#)
        End Sub

        In this example, the TestCall procedure wraps the # qualifer around the
        date. What is the equivalent in C#

        public void Test (DateTime datTest)
        {
        MessageBox.Show (datTest.ToStri ng());
        }

        public void TestCall()
        {
        this.Test(#1/1/2001#);
        }

        I have looked at dozens of samples on-line and multiple books, but nothing
        shows how to do this and it is starting to irritate me. This is basic stuff,
        but it is not documented anywhere that Google can find...


        "Alberto Poblacion" wrote:
        "James" <bondrulesit200 8@gmail.comwrot e in message
        news:329a49e8-f46f-409c-aaea-47c7ba46c6c5@x3 5g2000hsb.googl egroups.com...
        I am a beginner in C# programming and I just want to know how we can
        pass a datetime variable in a method. can anyone please help me
        regarding this.
        >
        I assume that you already know how to declare the method and that your
        problem is that you don't know how to insert into your code a given date,
        such as "September 28th, 2008". One of the ways to do it is by means of one
        of the constructors of the DateTime class, which takes year, month and day
        as parameters:
        >
        DateTime dateToPass = new DateTime(2008,9 ,28);
        MyMethod(dateTo Pass);
        .....
        >
        >
        private void MyMethod(DateTi me argument)
        {
        //Here 'argument' contains the date that you passed.
        }
        >
        >
        >

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: How to pass datetime variable

          Rob Jarrett <RobJarrett@dis cussions.micros oft.comwrote:
          Do you know how to pass date arguments in C#?
          That's not your actual problem - passing DateTime arguments is the same
          in C# as in VB. The problem is that C# doesn't have DateTime literals,
          which is what your #1/1/2001# is. Change that to new
          DateTime(2001, 1, 1) and your code will compile and work fine.

          --
          Jon Skeet - <skeet@pobox.co m>
          Web site: http://www.pobox.com/~skeet
          Blog: http://www.msmvps.com/jon.skeet
          C# in Depth: http://csharpindepth.com

          Comment

          • Mel Weaver

            #6
            Re: How to pass datetime variable

            many ways just in the help look up DateTime structure

            this.Test(DateT ime.Today);

            DateTime dt = new DateTime(2007,0 1,01);
            this.Test(dt);

            DateTime dt = DateTime.Parse( "10 Jun 2008");
            this.Text(dt);



            "Rob Jarrett" <RobJarrett@dis cussions.micros oft.comwrote in message
            news:DFDE0E41-7A37-4236-9390-C0B9B4E3A22A@mi crosoft.com...
            Do you know how to pass date arguments in C#?
            >
            VB Example
            >
            Public Sub Test (datTest As Date)
            MsgBox(datTest. ToString())
            End Sub
            >
            Public Sub TestCall()
            Me.Test(#1/1/2001#)
            End Sub
            >
            In this example, the TestCall procedure wraps the # qualifer around the
            date. What is the equivalent in C#
            >
            public void Test (DateTime datTest)
            {
            MessageBox.Show (datTest.ToStri ng());
            }
            >
            public void TestCall()
            {
            this.Test(#1/1/2001#);
            }
            >
            I have looked at dozens of samples on-line and multiple books, but nothing
            shows how to do this and it is starting to irritate me. This is basic
            stuff,
            but it is not documented anywhere that Google can find...
            >
            >
            "Alberto Poblacion" wrote:
            >
            >"James" <bondrulesit200 8@gmail.comwrot e in message
            >news:329a49e 8-f46f-409c-aaea-47c7ba46c6c5@x3 5g2000hsb.googl egroups.com...
            I am a beginner in C# programming and I just want to know how we can
            pass a datetime variable in a method. can anyone please help me
            regarding this.
            >>
            > I assume that you already know how to declare the method and that your
            >problem is that you don't know how to insert into your code a given date,
            >such as "September 28th, 2008". One of the ways to do it is by means of
            >one
            >of the constructors of the DateTime class, which takes year, month and
            >day
            >as parameters:
            >>
            >DateTime dateToPass = new DateTime(2008,9 ,28);
            >MyMethod(dateT oPass);
            >.....
            >>
            >>
            >private void MyMethod(DateTi me argument)
            >{
            > //Here 'argument' contains the date that you passed.
            >}
            >>
            >>
            >>

            Comment

            Working...