Reputation: 21261
I call my script like this:
>Driver.exe 26268 "01-01-2011" "02-01-2011"
arg 0 : c:\Services\JasperBatchService\Release\JasperBatchDriver.exe
arg 1 : 26268
Unhandled Exception: System.FormatException: String was not recognized as a valid DateTime.
at System.DateTimeParse.ParseExact(String s, String format, DateTimeFormatInfo dtfi, DateTimeStyles style)
at Program.main(String[] args) in C:\sswork\dev\fSharpServices\ops-Projects\JasperBatchDriver\Program.fs:line 65
and this is the relavent code:
let mutable argNum = 0
let cmdArgs = System.Environment.GetCommandLineArgs()
for arg in cmdArgs do
match argNum with
| 1 -> pmID <- System.Int32.Parse arg
| 2 -> startDate <- DateTime.ParseExact(arg, "D", new CultureInfo("en-US"))
| 3 -> endDate <- DateTime.ParseExact(arg, "D", new CultureInfo("en-US"))
| _ -> ()
printfn "arg %d : %s" argNum arg
argNum <- argNum + 1
i've also tried this:
for arg in cmdArgs do
match argNum with
| 1 -> pmID <- System.Int32.Parse arg
| 2 -> startDate <- DateTime.ParseExact(arg, "MM-dd-yyyy", new CultureInfo("en-US"))
| 3 -> endDate <- DateTime.ParseExact(arg, "MM-dd-yyyy", new CultureInfo("en-US"))
| _ -> ()
printfn "arg %d : %s" argNum arg
argNum <- argNum + 1
and this:
for arg in cmdArgs do
match argNum with
| 1 -> pmID <- System.Int32.Parse arg
| 2 -> startDate <- DateTime.ParseExact(arg, "MM/dd/yyyy", new CultureInfo("en-US"))
| 3 -> endDate <- DateTime.ParseExact(arg, "MM/dd/yyyy", new CultureInfo("en-US"))
| _ -> ()
printfn "arg %d : %s" argNum arg
argNum <- argNum + 1
the closest it seems I came is by using this:
(startDate).ToString("MM/dd/yyyy")
this however added double-quotes to the output....
Upvotes: 0
Views: 632
Reputation: 2382
this works (from fsi):
System.DateTime.ParseExact ("01-01-2011", "MM-dd-yyyy",
System.Globalization.CultureInfo "en-US");;
val it : System.DateTime = 1/1/2011 12:00:00 AM {Date = 1/1/2011 12:00:00 AM;
Day = 1;
DayOfWeek = Saturday;
DayOfYear = 1;
Hour = 0;
Kind = Unspecified;
Millisecond = 0;
Minute = 0;
Month = 1;
Second = 0;
Ticks = 634294368000000000L;
TimeOfDay = 00:00:00;
Year = 2011;}
how are you iterating through the args array? isn't the mutable argNum always 0?
Upvotes: 1
Reputation: 655
It looks like your calls to DateTime.ParseExact are expecting the "D" (Long Date pattern) format, which is different than what you're providing. If you plan on accepting arguments in the MM-DD-YYYY format, you'll need to use a custom format instead.
Try this:
DateTime.ParseExact(arg, "MM-dd-yyyy", new CultureInfo("en-US"))
Upvotes: 4