1

Can some one guide me on why I am getting this error. I have tried many permutations but I am not able to parse the string and get the date out of it

Public Function Starter(starterInput As LoginUserResponse) As List(Of GraphDataObj) Implements iSMS_Rest.Starter
Dim _currentUser = System.Web.HttpContext.Current.Cache(starterInput.tokenProp)
Dim res As Date
Dim provider As Globalization.CultureInfo = Globalization.CultureInfo.InvariantCulture
Dim dateString As String
dateString = starterInput.dateProp  // VALUE HERE: "2016-08-31T03:59:59.000Z"
res = Date.ParseExact(dateString, "dd/MM/yyyy", provider)
'SOME CODE
End Function

Exception I am getting

enter image description here

I looked into this link for help: Convert a string to a datetime

Community
  • 1
  • 1
Unbreakable
  • 6,201
  • 15
  • 62
  • 126
  • The string `"2016-08-31T03:59:59.000Z"` is not of the format `"dd/MM/yyyy"`. `ParseExact` really means *exact*. – David Aug 15 '16 at 18:23
  • `ParseExact` means you promise to tell it ***exactly*** what the format looks like. `"dd/MM/yyyy"` is not at all like `2016-08-31T03:59:59.000Z` – Ňɏssa Pøngjǣrdenlarp Aug 15 '16 at 18:24
  • @David: Can you kindly guide me then how can I fetch the date in the dd/MM/yyyy format please. – Unbreakable Aug 15 '16 at 18:24
  • @Unbreakable: By inputting a date in that format. For example, `"15/08/2016"`. The value is being provided to the method, this code isn't generating the value. If you want to provide a different value, then provide a different value. – David Aug 15 '16 at 18:25
  • @David: Thank you for reply. But I could not understand what you mean. In my REST Service I am getting date in `"2016-08-31T03:59:59.000Z"`. Can I some how parse it in "dd/MM/yyyy" format. – Unbreakable Aug 15 '16 at 18:27
  • 1
    `Dim dt = DateTime.Parse("2016-08-31T03:59:59.000Z")` its already in a format it can read/parse – Ňɏssa Pøngjǣrdenlarp Aug 15 '16 at 18:29

1 Answers1

1

The value you're using is:

"2016-08-31T03:59:5‌​9.000Z"

Which is not the format:

"dd/MM/yyyy"

It sounds like ParseExact isn't what you're looking for. The input format is a very common date format and will be understood by simply using Parse:

res = Date.Parse(dateString, provider)

(Or, if you don't trust the consistency of the format from the value being provided, you can also use Date.TryParse() instead.)

David
  • 176,566
  • 33
  • 178
  • 245
  • Thank you so much for your reply. I trust the consistency because I know how the date is getting passed from from end as I only wrote that code too and I know the input format wil always be like that. Currently from your answer I am getting date as #8/30/2016 11:59:59 PM# . Can I somehow get only in the required format. Do I have any in built methd which can parse it or trim it only #8/30/2016#. – Unbreakable Aug 15 '16 at 18:49
  • @Unbreakable: What do you mean by "getting date as #8/30/2016 11:59:59 PM#"? The question says the input to this method is "2016-08-31T03:59:5‌​9.000Z". All this operation results in is a `Date` object. `Date` objects have no formatting. You can *output* a `Date` however you like, but in the question you're not outputting anything at all. – David Aug 15 '16 at 18:51
  • I am sorry for not being clear. I debugged at checked the date that I got after I wrote code as `res = Date.Parse(dateString, provider)` – Unbreakable Aug 15 '16 at 18:52
  • I used your suggestion and put that code and then debugged it to see what output its giving. – Unbreakable Aug 15 '16 at 18:53
  • @Unbreakable: And what exactly is the problem then? Are you asking about the difference in the values? That looks like it's a timezone difference. – David Aug 15 '16 at 18:53
  • Thank you for being patient with me. Okey here is the thing. I need to pass the date received to another function which accepts date in #8/30/2016# format. So currently I got #8/30/2016 11:59:59 PM#. Since I need to pass only #8/30/2016# format, I need to trim down the trailing part 11:59:59 PM#. So basically my other function will only work if it gets date as #8/30/2016# WITHOUT any "time" stuff attached to it. – Unbreakable Aug 15 '16 at 18:57
  • @Unbreakable: How are you passing this to the other function? None of that is included in the question. If you're converting the `Date` to a string then that's where you'd apply your formatting. – David Aug 15 '16 at 18:59
  • I get what you are saying. Everything is working perfectly fine. I checked the "another function" is accepting "DATE" as the datatype. So I can always pass `#8/30/2016 11:59:59 PM#"`. I don't need to trim anything. Thank you so much and sorry for the confusion. – Unbreakable Aug 15 '16 at 19:06