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

Convert date from '/' to '-'

1 Answer 28 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Federico
Top achievements
Rank 1
Federico asked on 18 Jan 2019, 11:42 AM

Oracle DB returns this TimeStamp: "2018-11-07T00: 00: 00.000"

I would like to convert it to "2018/11/07T00: 00: 00.000"

and then convert it to 07/11/2018 (dd / MM / yyyy)

How can I do ?


1 Answer, 1 is accepted

Sort by
0
Marin Bratanov
Telerik team
answered on 21 Jan 2019, 10:13 AM
Hi Federico,

If there isn't a helper library to help you with date conversion (the Parse, and ParseExact methods of the DateTime object are quite flexible), you can do it with relatively simple string operations. Assuming C# is the language, and MVC the backend, here is a basic example and the result from it is attached below.

public ActionResult Index()
{
    string oracleDate = "2018-11-07T00: 00: 00.000";
    string dateWithSlashes = oracleDate.Replace('-', '/');
    string[] dateSegments = dateWithSlashes.Split('/');
    DateTime actualDate = new DateTime(int.Parse(dateSegments[0]), int.Parse(dateSegments[1]), int.Parse(dateSegments[2].Split('T')[0]));
    string finalDate = actualDate.ToString("dd / MM / yyyy"); //note the spaces around the slashes, as per the format provided in the question
 
    ViewData["origDate"] = oracleDate;
    ViewData["withSlashes"] = dateWithSlashes;
    ViewData["desiredResult"] = finalDate;
    ViewData["actualDateObject"] = actualDate;
 
    return View();
}
@ViewData["origDate"]
<br />
@ViewData["withSlashes"]
<br />
@ViewData["desiredResult"]
<br />
@ViewData["actualDateObject"]


Regards,
Marin Bratanov
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
General Discussions
Asked by
Federico
Top achievements
Rank 1
Answers by
Marin Bratanov
Telerik team
Share this question
or