Cast string to date with specified pattern

3 posts, 0 answers
    ANDREY avatar
    10 posts
    Member since:
    Nov 2016

    Posted 28 Mar 2019 Link to this post

    Hello, I have a string representation of the date - "01.02.2016", and I need to cast it to datetime with specific pattern "dd.MM.yyyy" to prevent wrong casting in another culture settings (e.g. en-us which will interpret it by pattern "MM.dd.yyyy").

    How can I do this?

    Thanks a lot!

  2. Ivan Hristov
    Ivan Hristov avatar
    227 posts

    Posted 01 Apr 2019 Link to this post

    Hello Andrey,

    There is no a built-in way of providing the format when parsing a date. The recommended solution would be to create a User Function that accepts the string as argument and returns a DateTime instance according to the specified pattern. Another option is to convert the string to Date using the Date(year, month, day) function. The arguments can be provided using the Substr() function and each substring must be converted to an integer with CInt() to satisfy the Date() function requirements. In this case the expression would look like this:

    =Date(CInt(Substr(Fields.DateStr, 6, 4)), CInt(Substr(Fields.DateStr, 3, 2)), CInt(Substr(Fields.DateStr, 0, 2)))

    Hope this helps.

    Ivan Hristov
    Progress Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
    ANDREY avatar
    10 posts
    Member since:
    Nov 2016

    Posted 17 Apr 2019 in reply to Ivan Hristov Link to this post

Back to Top