Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a DateTime Variable to a Console Application using Cmd Prompt

Tags:

c#

.net

dos

I hope you can help.

I have a console application that needs to receive a datetime value, so for example 2015-12-01 00:00:00.000, but the time part does not get picked up because of the space between the date and the time. I have 3 variables I am passing : (2 integers and 1 Datetime)

Cmd Prompt :

C:\Application1.exe 3935 1 2015-12-01 00:00:00.000

Is there a way I can pass through the date and time as 1 variable, like '2015-12-01 00:00:00.000' ?

Ive tried everything, but nothing seems to work.

like image 435
AxleWack Avatar asked Jan 26 '16 14:01

AxleWack


2 Answers

Sure, encapsulate it into quotes and parse it from a string:

var myDate = DateTime.Parse(args[2]);

Or a saver appraoch would be using DateTime-TryParse:

var date = DateTime.Now;
if (DateTime.TryParse(out date)) { /* do anything with the date */ }

Call it like:

C:\Application1.exe 3935 1 "2015-12-01 00:00:00.000"
like image 133
MakePeaceGreatAgain Avatar answered Oct 27 '22 08:10

MakePeaceGreatAgain


Add a T:

C:\Application1.exe 3935 1 2015-12-01T00:00:00.000

This should work if you use DateTime.Parse to convert and you won't have to mess with spaces.

like image 44
Ofiris Avatar answered Oct 27 '22 08:10

Ofiris