web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

get the date from string

(1) ShareShare
ReportReport
Posted on by

Hello,

I want to get the date from string, but for every regional setting.

i.e the setting may be dd/mm/yyyy or mm.dd.yyyy or yy.mm.dd - each of them can be represented as string, but I don't know what is the regional setting, and still want to convert from any of above (of the current setting).

How can I do that?

Thanks :)

*This post is locked for comments

I have the same question (0)
  • Martin Dráb Profile Picture
    236,394 Most Valuable Professional on at
    RE: get the date from string

    If you don't know which format to use, the task generally can't be done. For example, you can't tell whether 01/02/2014 means 1st February or 2nd January. You need to know the format.

  • Suggested answer
    keoma Profile Picture
    32,729 on at
    RE: get the date from string

    hi,

    you can use .net libs.

    string strDate = "10/15/2014";

    System.Globalization.CultureInfo cultureInfo = System.Globalization.CultureInfo.GetCultureInfo("en-US");

    string dateFormat = cultureInfo.DateTimeFormat.ShortDatePattern;

    System.DateTime date = DateTime.Parse(strDate, cultureInfo);

    cultureInfo = System.Globalization.CultureInfo.GetCultureInfo("de-DE");

    dateFormat = cultureInfo.DateTimeFormat.ShortDatePattern;

    date = DateTime.Parse(strDate, cultureInfo);

    follow stackoverflow.com/.../weekofyear-in-x-using-net-lib

  • Community Member Profile Picture
    on at
    RE: get the date from string

    Hi,

    maybe you can use the .NET class CultureInfo and DateTimeFormatInfo (property of CultureInfo) to determine which format to use.

    There is a property named CultureInfo.CurrentCulture to determine which is the currently use culture.

    Greetings

    Nils

    Edit: jonathan was faster :-)

  • Suggested answer
    Bashir Ahmad Profile Picture
    5,248 on at
    RE: get the date from string

    The date and time can be different on the client and the server.

    You will not typically need to format a date to a string. Use date fields, variables, and date controls on forms and reports instead.

    If you need to format a date to a string:

       For user interface situations, use strFmt or date2Str with -1 in all the formatting parameters. This ensures that the date is formatted in the way that the user has specified in Regional Settings.

       For other specific system-related situations, such as communication with external systems, use date2Str.

    When you let Regional Settings dictate the format, be aware that it can change from user to user and might not be a suitable format for external communication.

    Using str2Date indicates that dates are being used that have had a string format.

  • André Arnaud de Calavon Profile Picture
    299,399 Super User 2025 Season 2 on at
    RE: get the date from string

    For all solutions: The problem remains like Martin mentioned:

    01-02-2014 how to determine if the first column is the day or month...

    If you get a date from somewhere, the source and thus the format should be available...

  • Suggested answer
    Bashir Ahmad Profile Picture
    5,248 on at
    RE: get the date from string

    in this case a little work around required.

    1. first we need to convert string to any of date formate: date str2Date(str _text, str _sequence)

    then that date return from above function date str2Date we can get the required digits Day, month or year of that date.

    //Get day from date

    dayOfMth(str2Date(str _text, str _sequence))

    //Get month from date

    mthOfYr(str2Date(str _text, str _sequence))

    //Get year from date

    year(str2Date(str _text, str _sequence))

    sorry in your case sequence is the issue :(

  • Suggested answer
    Simon Buxton Profile Picture
    130 on at
    RE: get the date from string

    You could use the code:

    FromDate fromDate;

    FromDate fromDate;

    utcDateTime utcDate;

    fromDate = System.DateTime::Parse("01/12/2014");

    utcDate = System.DateTime::Parse("01/12/2014");

    If you are going to work with the string version of the string, you should store the cultureInfo also (e.g. dd/MM/yyyy) or the actual date value.

    For example

    System.Globalization.CultureInfo cultureInfo;

    System.Globalization.DateTimeFormatInfo dateTimeFormatInfo;

    cultureInfo = System.Globalization.CultureInfo::get_CurrentCulture();

    dateTimeFormatInfo = cultureInfo.get_DateTimeFormat();

    info(dateTimeFormatInfo.get_ShortDatePattern());

    Just be careful about how the data is presented to users in different time zones, as this type of coding is not normally done.

    Regards,

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Community Member Profile Picture

Community Member 4

#1
Martin Tocauer Profile Picture

Martin Tocauer 4

#3
Nayyar Siddiqi Profile Picture

Nayyar Siddiqi 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans