Date conversion for field retrieved from dictionary data

I’m getting this below error:
Assign- start date modification: String was not recognized as a valid DateTime.
I’m fetching data from the dictionary of type(String,String), and a date field from it has to be converted to a specific date format M/d/yyyy h:mm:ss tt.

start_Date = DateTime.ParseExact(dictionaryData(“start_date” ), “M/d/yyyy h:mm:ss tt”, System.Globalization.CultureInfo.InvariantCulture)

start_Date is a Datetime variable.

Use the immediate panel

type in the dictionaryData(“start_date”)
Explore with DateTime Parseformat is needed
do prototyping of the changed statement also within the immediate panel
integrate the changes into your xaml

Hi @automated

The error you are encountering suggests that the date string you are trying to parse does not match the expected format.

What is the input format of the date and to which format do you want to convert the date?.

Regards

@automated,

Try this if your dictionary is of type Strinf,Object.

start_Date = DateTime.ParseExact(dictionaryData("start_date" ).ToString, "M/d/yyyy h:mm:ss tt", System.Globalization.CultureInfo.InvariantCulture)

Thanks,
Ashok :slight_smile:

Hi @ashokkarale
I did try the above solution , still it gave me the same error message.

dictionary datatype is (String,String).

Just share with us the output from immediate panel

@automated,

Ohh… ok.

In that case, try log message the date before you parse it.

The format string is "M/d/yyyy h:mm:ss tt". This means the date string should look something like this: "1/31/2024 2:15:30 PM".

Here are a few additional things you could check:

  1. Format: Ensure that the date string in dictionaryData("start_date") matches the format "M/d/yyyy h:mm:ss tt" exactly. Even a small discrepancy, like missing leading zeros or AM/PM indicator, can cause this error.
  2. Culture Info: The DateTime.ParseExact method is using System.Globalization.CultureInfo.InvariantCulture, which might not match the culture info of the date string. If the date string is formatted according to a specific culture, you should use that culture info instead.
  3. Null or Empty Value: Check if dictionaryData("start_date") is null or an empty string. Trying to parse a null or empty string will also throw this error.

LLM Helped me to write this answer.

Thanks,
Ashok :slight_smile:

Thanks,
let me try then your above suggestions.
but below is the output from the panel, which will show you the format of the date retrieved, and what format I want it to be changed to.

@automated,

You should be parsing the date with this:
start_Date = DateTime.ParseExact(dictionaryData("start_date" ).ToString, "MM/dd/yyyy hh:mm:ss", System.Globalization.CultureInfo.InvariantCulture)

Once Parsed then convert it to as you like

Thanks,
Ashok :slight_smile:

Hi @automated

Try this

DateTime.ParseExact(Input, "MM/dd/yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture)

If you having different formats then try below

DateTime.ParseExact(Input,{"MM/dd/yyyy HH:mm:ss","M/d/yyyy HH:mm:ss"}, System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.None)

Regards,

Perfect, as we suggested

as you can See the Parser Format hast to be adapted

Just proceed with next suggested steps

Hello @Parvathy

I want to convert to this format :point_right: “M/d/yyyy h:mm:ss tt”

The existing format of date in dictionary is : “05/31/2024 23:00:00”

@automated

DateTime.ParseExact(Input, "MM/dd/yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture)

Parser IT AS mentioned above and add toString(“M/d/yyyy h:mm:ss tt”)

DateTime.ParseExact(....).toString("M/d/yyyy h:mm:ss tt")

@automated,

I think issue with your input string. It contains unnecessary string. Clean it first and then Parse.

strInput = strInput.Split("Dict date is :-")(1).Trim

Sample Code:
demo.xaml (7.4 KB)

Thanks,
Ashok :slight_smile: )

Hello All,

I was able to solve the issue using the below function:

Convert.ToDateTime(dict_startdate.ToString).

Thank you all for your inputs and help.

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