Get the system date and split day, month and year

后端 未结 5 929
猫巷女王i
猫巷女王i 2020-12-06 18:18

My system date format is dd-MM-yyyy(20-10-2012) and I\'m getting the date and using separator to split the date, month and year. I need to convert date format (dd/MM/yyyy) w

相关标签:
5条回答
  • 2020-12-06 18:51

    You can do like follow:

     String date = DateTime.Now.Date.ToString();
        String Month = DateTime.Now.Month.ToString();
        String Year = DateTime.Now.Year.ToString();
    

    On the place of datetime you can use your column..

    0 讨论(0)
  • 2020-12-06 18:57

    You can split date month year from current date as follows:

    DateTime todaysDate = DateTime.Now.Date;

    Day:

    int day = todaysDate.Day;

    Month:

    int month = todaysDate.Month;

    Year:

    int year = todaysDate.Year;

    0 讨论(0)
  • 2020-12-06 19:01

    You should use DateTime.TryParseExcact if you know the format, or if not and want to use the system settings DateTime.TryParse. And to print the date,DateTime.ToString with the right format in the argument. To get the year, month or day you have DateTime.Year, DateTime.Month or DateTime.Day.

    See DateTime Structures in MSDN for additional references.

    0 讨论(0)
  • 2020-12-06 19:10

    Here is what you are looking for:

    String sDate = DateTime.Now.ToString();
    DateTime datevalue = (Convert.ToDateTime(sDate.ToString()));
    
    String dy = datevalue.Day.ToString();
    String mn = datevalue.Month.ToString();
    String yy = datevalue.Year.ToString();
    

    OR

    Alternatively, you can use split function to split string date into day, month and year here.

    Hope, it will helps you... Cheers. !!

    0 讨论(0)
  • 2020-12-06 19:14

    Without opening an IDE to check my brain works properly for syntax at this time of day...

    If you simply want the date in a particular format you can use DateTime's .ToString(string format). There are a number of examples of standard and custom formatting strings if you follow that link.

    So

    DateTime _date = DateTime.Now;
    var _dateString = _date.ToString("dd/MM/yyyy");
    

    would give you the date as a string in the format you request.

    0 讨论(0)
提交回复
热议问题