Show date and time in SQL format

Padmanabhan, Venkatesh 120 Reputation points
2023-04-10T11:06:20.99+00:00

Hi. I am trying to show the difference of time between current time and what I get back from the data table using C#. I am filling the data table from AS 400 system and the date and time are shown in the format of : Date : 1211210 ( these are based on century marker ) Time : 73001 .How to show the date and time in the SQL format and show the difference in minutes between the time received and the current time ? Thanks

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,364 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 35,556 Reputation points Microsoft Vendor
    2023-04-10T11:52:34.7633333+00:00

    Hi @Padmanabhan, Venkatesh, welcome to Microsoft Q&A.

    You only need to understand the format principle of ToString("yyyy-MM-dd HH:mm:ss")); . So, you could do as follows.

    using System;
    
    namespace ConsoleApp3
    {
        internal class Program
        {
            static void Main(string[] args)
            {
                //just for testing
                int date = 1211210;
                int time = 73001;
    
                int year = date / 10000 + 1900; //  (e.g. 2021)
                int month = date % 10000 / 100; //  (e.g. 12)
                int day = date % 100; //            (e.g. 10)
                int hour = time / 10000; //         (e.g. 7)
                int minute = time % 10000 / 100; // (e.g. 30)
                int second = time % 100; //         (e.g. 1)
    
                DateTime as400DT = new DateTime(year, month, day, hour, minute, second);
                DateTime currentDT = DateTime.Now;
    
                // Calculate the time difference in minutes
                TimeSpan timeDifference = currentDT - as400DT;
                int timeDifferenceMinutes = (int)timeDifference.TotalMinutes;
    
                // Display the results
                Console.WriteLine("AS400 Date and Time: " + as400DT.ToString("yyyy-MM-dd HH:mm:ss"));
                Console.WriteLine("Current Date and Time: " + currentDT.ToString("yyyy-MM-dd HH:mm:ss"));
                Console.WriteLine("Time Difference (Minutes): " + timeDifferenceMinutes);
    
                Console.ReadLine();
            }
        }
    }
    

    enter image description here

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful