DateTime return value problem


Looking for solution, where I can assign DateTime response in format of (yyyy-MM-ddThh-mm-ss.bbbZ to DateTime variable so for example
startDate = “2017-01-01T23:28:59.564Z”, thx in advanced.

Best Regards, Jouni

Hi @jouni.riimala,

this is ISO 8601 format


True, wondering how to assign return value to datetime variable as I should use variable in future api call and api documentation dictate that it need to be in datetime format…

Tried firstly to assign your suggestion directly to datetime variable, but obviously it’s not working as string cannot be assigned as such to datetime variable… After this I tried to Convert.ToDateTime(string variable) but also that didn’t worked based on output
Assign, convert string to datetime: String was not recognized as a valid DateTime.

Use Datetime.ParseExact. In Page below, look for “Parse a string representing UTC”.
dateValue = Date.ParseExact(dateString, "o", CultureInfo.InvariantCulture, DateTimeStyles.None)

Thx, still something wrong in my string assignment. Pls check main.xaml and indicate what I’m doing wrong.

Main.xaml (6.4 KB)

Well, the time format in your opening post in not the same in your code.

"yyyy-MM-ddThh-mm-ss.bbbZ" is not

To keep consistency, let’s keep dateFormat as a variable:

dateFormat As String = "yyyy-MM-ddTHH:mm:ssZ"
culture As CultureInfo = System.Globalization.CultureInfo.InvariantCulture
dateStyle As DateTimeStyles = System.Globalization.DateTimeStyles.None

startString As String =
startDate As DateTime = Date.ParseExact(startString, dateFormat, culture, dateStyle)

endString As String = DateTime.Now.AddHours(10).ToString(dateFormat)
endDate As DateTime = Date.ParseExact(endString, dateFormat, culture, dateStyle)

Thx from your help and got it working. Minor adjustment what I still did was to change string to use .UtcNow instead of local .now. Done based on thinking that in format string Z refers to UTC.

@jouni.riimala, no problem.

Yes, Z refers to UTC in that datetime format but it is just a string output formating, not a datetime conversion.

UtcNow is timezone aware and will consider the datetime at Z but doesn’t prevent you to manipulate it’s string representation.

For some timezone awareness, take a look at DateTime.Kind

And you’re right, to be consistent, I’d rather use:

dateFormat As String = "yyyy-MM-ddTHH:mm:ssZ"
startString As String = DateTime.UtcNow.ToString(dateFormat)

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.