473,396 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.
Sep 27 '08 #1
5 13760
Hi James,

welcome to the great world of programming!

Here is a example:

public void PrintDateAndTime(System.DateTime DateAndTimeToPrint)
{
System.Console.WriteLine("Actual Date and Time: {0}",
DateAndTimeToPrint.ToString());
}

you invoke it that way:

PrintDateAndTime(System.DateTime.Now);

Asuming that you are working on a Console!

You should read the basics about functions/members/methods:
[Methods (C# Programming Guide)]
http://msdn.microsoft.com/en-us/library/ms173114.aspx

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

[C# Programming Guide]
http://msdn.microsoft.com/en-us/library/67ef8sbd.aspx

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."

Sep 27 '08 #2
"James" <bo*************@gmail.comwrote in message
news:32**********************************@x35g2000 hsb.googlegroups.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(dateToPass);
.....
private void MyMethod(DateTime argument)
{
//Here 'argument' contains the date that you passed.
}
Sep 28 '08 #3
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.ToString());
}

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" <bo*************@gmail.comwrote in message
news:32**********************************@x35g2000 hsb.googlegroups.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(dateToPass);
.....
private void MyMethod(DateTime argument)
{
//Here 'argument' contains the date that you passed.
}
Oct 2 '08 #4
Rob Jarrett <Ro********@discussions.microsoft.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 - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Oct 2 '08 #5
many ways just in the help look up DateTime structure

this.Test(DateTime.Today);

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

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

"Rob Jarrett" <Ro********@discussions.microsoft.comwrote in message
news:DF**********************************@microsof t.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.ToString());
}

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" <bo*************@gmail.comwrote in message
news:32**********************************@x35g200 0hsb.googlegroups.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(dateToPass);
.....
private void MyMethod(DateTime argument)
{
//Here 'argument' contains the date that you passed.
}

Oct 2 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Rigga | last post by:
Hi I would like to pass a variable to an external program i.e. os.system('cd $variable') However this doesnt work, I cant figure it out, any ideas? Cheers
2
by: Cory | last post by:
I'm using ASP to run a batch file. I need to know how to pass a variable to this .bat file. Is this possible? I'm using the following code but need to know how to pass ASP variable. set...
5
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! When reading a value from an SQL database i get to a DateTime variable... How can i check that the there is no value assigned? Since i cant use if(myDate.ShortDate != null) ? Regards
1
by: Alpha | last post by:
When I select a datetime variable into a dataset does that stay a datetime column? I specified it to be so in the dataset but having problem formating it in the Crystal. Thanks, Alpha ...
2
by: Solution Seeker | last post by:
Hi, I need a solution to assign a Null to a Datetime Variable / Object. Am willing to know whether it is possible? I will appreciate you if any one knows the solution for it . The...
2
by: tshad | last post by:
I can't seem to get the Date portion of a datetime variable put into a string: I tried: DateTime dateTime; dateTime = Convert.ToDateTime(dr).Date; stemp = Convert.ToString(dateTime); This...
3
by: Reza Solouki | last post by:
Hello, I have a system where it gets its data from a file that is provided periodically. There are cases where many values such as dates are blank. Considering DateTime variable doesn't accept...
3
by: jonnyblazed | last post by:
I've searched everywhere for this but I'm not sure exactly what to search for. What I am trying to do is pass a variable to a php script via the URL. However, I don't want to use the standard...
5
by: cpnet | last post by:
I'm able to pass string parameters with no problems to a LocalReport being displayed by the ASP.NET ReportViewer control. However, my report also has a DateTime parameter. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.