This topic is locked

Formatting datetime as a string in

2/6/2023 7:35:44 AM
ASPRunner.NET General questions
david powell author

HI All,

I have searched for an answer for this and although ti is really basic can find no pointers, except a solution for PHP.
I am using ASPRunner.Net, hence C#, and creating a custom field in the 'view as' option on a form.

But I am struggling to convert the ["dob"] (which is a datetime field on the database) into a custom text format:


value = data["firstname"].ToString() + " " + data["lastname"].ToString() + " " + value = data["dob"].ToString("dd-mm-yyyy");

...should work according to c# resources, but throws a compilation error of 'include\CustomExpressions.cs(34,90): error CS1501: No overload for method 'ToString' takes 1 arguments'

This is a PHP solution, but neither date nor strtotime are available under c#; the alternative to strtotime given is ToString("dd-mm-yyyy");

$value = date('D/d/M/Y', strtotime($value)

I do find dates quite challenging - there never seems an easy solution to achieve what one wants!

What am a I doing wrong?

Many thanks!

jadach 2/6/2023

Might be easier to build that in your SQL Editor.

david powell author 2/6/2023

I agree!! that is the approach I have taken before, but I just thought there has to be a way to format date times into a string! All other aspects of java and c# seem to cover everything I need.... except for managing dates which seem to be a black art.....

david powell author 2/7/2023

Either its too straightforward or there is no answer!

I am going to go back to using good old mysql to create the necessary string (which it does so easily - date_format(dob, '%d/%m/%Y') AS dobstring ), and when I have a moment write a function myself to provide some datetime utilties.....

jadach 2/7/2023

This should work for view as custom

value = data["firstname"].ToString() + ' ' + data["lastname"].ToString() + ' ' + DateTime.Parse(data["dob"]).ToString("dd-mm-yyyy");