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) whether the formats returns at any date format.
string sDate = string.Empty;
DateTime _date = DateTime.Now;
int count = 0;
string format = "dd-MM-yyyy";
sDate = dateFormat.ToString(format);
string[] Words = sDate.Split(new char[] { '-' });
foreach (string Word in Words)
{
count += 1;
if (count == 1) { Day = Word; }
if (count == 2) { Month = Word; }
if (count == 3) { Year = Word; }
}
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.
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;
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. !!
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..
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With