473,654 Members | 3,096 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 13849
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)]
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.comwrot e in message
news:32******** *************** ***********@x35 g2000hsb.google groups.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.
}
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.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" <bo************ *@gmail.comwrot e in message
news:32******** *************** ***********@x35 g2000hsb.google groups.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.
}
Oct 2 '08 #4
Rob Jarrett <Ro********@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 - <sk***@pobox.co m>
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(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" <Ro********@dis cussions.micros oft.comwrote in message
news:DF******** *************** ***********@mic rosoft.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" <bo************ *@gmail.comwrot e in message
news:32******* *************** ************@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.
}

Oct 2 '08 #6

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

Similar topics

5
7278
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
6497
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 wshell = server.createobject("wscript.shell") wshell.run "c:\file.bat" set wshell = nothing
5
31187
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
1479
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 dtFinancial = this.dtpFinancial.Value; dtFinancialEnd = this.dtpFinancialEnd.Value;
2
2405
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 Situation is
2
6716
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 will give me "5/5/2006 12:00:00 AM"
3
7410
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 null, and SQL Server doesn't accept DateTime.MinValue... What is the solution to deal with these kind of cases? I probably could create some work around to send some awkword value that SQL accept and then
3
2033
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 "?name=value format." I want to either pass the variable as "value website.com" or "website.com/value" where the script is stored in some directory "website.com/script/index.php" and the value gets stored in a variable declared within my php script.
5
10152
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. LocalReport.SetParameters(..) only seems to allow me to set string parameters. Is there a way to pass DateTime (or other types) of parameters? Or can I only use string parameters in a LocalReport if I need to pass them in from my ASP.NET app? Thanks, cpnet
0
8372
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8706
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8475
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8591
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5621
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4293
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1915
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1592
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.