Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX (Archived)

get the date from string

Posted on by Microsoft Employee

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

  • Suggested answer
    Simon Buxton Profile Picture
    Simon Buxton 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,

  • Suggested answer
    Bashir Ahmad Profile Picture
    Bashir Ahmad 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 :(

  • André Arnaud de Calavon Profile Picture
    André Arnaud de Cal... 291,280 Super User 2024 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
    Bashir Ahmad 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.

  • Community Member Profile Picture
    Community Member Microsoft Employee 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
    keoma Profile Picture
    keoma 32,675 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

  • Martin Dráb Profile Picture
    Martin Dráb 230,214 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.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans