This is a migrated thread and some comments may be shown as answers.

Cast string to date with specified pattern

2 Answers 824 Views
Report Designer (standalone)
This is a migrated thread and some comments may be shown as answers.
ANDREY
Top achievements
Rank 1
ANDREY asked on 28 Mar 2019, 09:58 AM

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 Answers, 1 is accepted

Sort by
0
Ivan Hristov
Telerik team
answered on 01 Apr 2019, 03:37 PM
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.

Regards,
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
Vishal
Top achievements
Rank 1
commented on 18 Jul 2022, 01:00 PM

Hi Ivan,

What if Fields.DateStr is null. It gives input string is not in correct format. Can we use default value?

Todor
Telerik team
commented on 21 Jul 2022, 08:43 AM

Hi Vishal,

You may use the Null-coalescing Logical operator (??) or IsNull(value, defaultValue) Conditional Function.

0
ANDREY
Top achievements
Rank 1
answered on 17 Apr 2019, 10:47 AM
Thanks!
Tags
Report Designer (standalone)
Asked by
ANDREY
Top achievements
Rank 1
Answers by
Ivan Hristov
Telerik team
ANDREY
Top achievements
Rank 1
Share this question
or