Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a program that returns date formatting errors for the data im submitting. When i do a request to the SOAP endpoint i get formatting errors like :
C#
2020-01-30 18:27:22,820|MethodFilter@ff7cd8      |SSException          Throwing com.systemsunion.util.datatype.formatting.InvalidExternalDateLengthException
    Message : The external date value '7252019' is not the correct length.  The correct format is DDMMYYYY.
Stack Trace : 
com.systemsunion.util.datatype.formatting.InvalidExternalDateLengthException: The external date value '7252019' is not the correct length.  The correct format is DDMMYYYY.
	at com.systemsunion.util.datatype.formatting.DateFormatter.getInternalFormat(Unknown Source)


and another as :
C#
2020-01-30 18:27:22,825|MethodFilter@ff7cd8      |SSException          Throwing com.systemsunion.util.datatype.formatting.InvalidExternalDateException
    Message : The external date value '10302019' is invalid.  The correct date format is DDMMYYYY.
Stack Trace : 
com.systemsunion.util.datatype.formatting.InvalidExternalDateException: The external date value '10302019' is invalid.  The correct date format is DDMMYYYY.
	at com.systemsunion.util.datatype.formatting.DateFormatter.getInternalFormat(Unknown Source)
.

Example source dates i have include :

2019-09-03T00:00:00

C#
2019-09-26T00:00:00

2019-11-26T00:00:00


and Expected Output in each case is :

03092019
26092019
26112019

What I have tried:

I have tried formatting the input in my source as below but i think the logic is not converting as correctly as expected:

C#
string dateSeparator = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.DateSeparator;
                            string dateTime = transaction.Cover_start_date;
                            DateTime dt = Convert.ToDateTime(dateTime);
                            var dateOnlyString = dt.ToShortDateString().Replace(dateSeparator, "");


where
transaction.Cover_start_date
is the source date from my model. Any help on what i might be missing. I am especially suspicious of the first exception where the error points to the converted date as
'7252019'
. As it is the all the dates coming in from the model are valid dates and im not entirely sure what was the original value of
'7252019'
when in was in its source form i.e
YYYY-MM-DDT12:48:15
Posted
Updated 6-Feb-20 1:22am

 
Share this answer
 
Comments
Maciej Los 6-Feb-20 6:48am    
Short And To The Point!
MadMyche 6-Feb-20 6:50am    
+5 (I am just speechless on this)
Another point to make in addition to Solution 1 (which is spot on). You have used
C#
DateTime dt = Convert.ToDateTime(dateTime);
Convert should be avoided. Use instead the Parse and ParseExact methods for types, in this case look at
- DateTime.Parse Method (System) | Microsoft Docs[^]
- DateTime.ParseExact Method (System) | Microsoft Docs[^]
- DateTime.TryParse Method (System) | Microsoft Docs[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900