Convert comma separated string to datetime - c#

The Exchange webservice has a method that takes the DateTime in the format below
appointment.Start = new DateTime(2014, 03, 04, 11, 30, 00);
I have a string which is formed by concatenating various fields to form the date my string is as below:
string date="2014,03,04,11,00,00"
But if i try to to parse my string as the date it gives the error "String was not recognized as a valid DateTime".
DateTime.Parse(date)

You can use DateTime.ParseExact:
string date = "2014,03,04,11,00,00";
DateTime dateTime = DateTime.ParseExact(date, "yyyy,MM,dd,HH,mm,ss", CultureInfo.CurrentCulture);

Try this :
string date = "2014,03,04,11,00,00";
DateTime datDate;
if(DateTime.TryParseExact(date, new string[] { "yyyy,MM,dd,hh,mm,ss" },
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None, out datDate))
{
Console.WriteLine(datDate);
}

Related

convert datetime passed into parameter to a string c#

I would like to convert a datetime passed into parameter into a string using this function but it doesn't work. Could anyone help me ? Thank you in advance
Here is my function
public string ConvertDateTimeToString(DateTime date)
{
string date_str = date.ToString("dd/MM/yyyy HH:mm:ss");
return date_str;
}
and after when i create a new dateTime object
DateTime dateTest = new DateTime(2008, 5, 1, 8, 30, 52);
ConvertDateTimeToString(dateTest);
Console.WriteLine(dateTest); /// show this {01/05/2008 08:30:52}
The "ConvertDateTimeToString" will create a new string.
The existing DateTime still untouched.
You have to use the new string in your Console.WriteLine :
var dateString = ConvertDateTimeToString(dateTest);
Console.WriteLine(dateString);

How to convert string date (yyyy) in to Date format in c#

How to Convert string date into DateTime and add days?
string strYear= "2020";
DateTime datetime = DateTime.ParseExact(strYear, "yyyy", CultureInfo.InvariantCulture);
datetime.addDays(10);
This works:
string strYear= "2020";
DateTime datetime = DateTime.ParseExact(strYear, "yyyy",System.Globalization.CultureInfo.InvariantCulture);
datetime = datetime.AddDays(10);

DateTime Format check

Quick Question
Value passed in ActivationDate or ExpirationDate string, must be in either of the two formats stated below:Format 1:YYYY-MM-DD & Format 2: YYYY-MM-DD HH:MM
If the date values are not in either of the above format, then it should report back appropriate error message.
Any clue? Thanks in advance
You can use DateTime.TryParseExact, using a string[] with the valid formats:
string[] formats = new string[] { "yyyy-MM-dd", "yyyy-MM-dd HH:mm" };
string s = "2017-12-01 12:23";
DateTime date;
bool converted = DateTime.TryParseExact(s, formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out date);
With this code, you get in convertedif the input date was in a valid format, and in date the parsed DateTime
You can use ParseExact() with try-catch:
string date = "2017-02-01";
DateTime dt = default(DateTime);
try
{
dt = DateTime.ParseExact(date, new string[] {"yyyy-MM-dd", "yyyy-MM-dd hh:mm"}, CultureInfo.InvariantCulture, DateTimeStyles.None);
}
catch (FormatException ex)
{
//error
}
OR
Use TryParseExact():
string date = "2017-02-01";
DateTime dt;
if (DateTime.TryParseExact(date, new string[] {"yyyy-MM-dd", "yyyy-MM-DD hh:mm"}, CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
{
//do something and use "dt" variable
}
else
{
//error
}

How to display date only from datetime type into datetime object

In string array I have date field, which I want to format for date only and bind it to date time object.
string[] strArray = new string[] {
"Mahesh Chand",
"Mike Gold",
"Raj Beniwal",
"Praveen Kumar",
"7/10/1974 7:10:24 AM"
};
DateTime dateFromString = Convert.ToDateTime(strArray[4]);
DateTime dt = DateTime.ParseExact(dateTimeString, "dd/MM/yyyy", null);
Am not getting only date. still am getting date and time.
You can take only the Date without Time from a DateTime object in this way:
DateTime dateFromString = Convert.ToDateTime(strArray[4]);
var onlyDate = dateFromString.Date;
// Display date using short date string.
Console.WriteLine(onlyDate .ToString("d"));
More info on MSDN
Use like this
string[] strArray = new string[] {
"Mahesh Chand",
"Mike Gold",
"Raj Beniwal",
"Praveen Kumar",
"7/10/1974 7:10:24 AM" };
DateTime dateFromString = Convert.ToDateTime(strArray[4]);
string dt= DateTime.ParseExact(dateTimeString, "dd/MM/yyyy", null).ToString("dd/MM/yyyy");
in this way you will get date as string, but if you want only date part as DateTime type then it isn't possible because in DateTime type if you don't specify any time then it will automatically add a default time as 12:00:00 AM(00:00:00) in DateTime
Update
you can use like this for all culture
string dt = DateTime.ParseExact(dateTimeString, "dd/MM/yyyy", CultureInfo.InvariantCulture)
.ToString("dd/MM/yyyy");
or you can use simply like this
string[] strArray = new string[] {
"Mahesh Chand",
"Mike Gold",
"Raj Beniwal",
"Praveen Kumar",
"7/10/1974 7:10:24 AM" };
string dateFromString = Convert.ToDateTime(strArray[4]).ToString("dd/MM/yyyy");

How convert Gregorian date to Persian date?

I want to convert a custom Gregorian date to Persian date in C#.
For example, i have a string with this contents:
string GregorianDate = "Thursday, October 24, 2013";
Now i want to have:
string PersianDate = پنج‌شنبه 2 آبان 1392 ;
or
string PersianDate = 1392/08/02
Thanks
Use the PersianCalendar:
string GregorianDate = "Thursday, October 24, 2013";
DateTime d = DateTime.Parse(GregorianDate);
PersianCalendar pc = new PersianCalendar();
Console.WriteLine(string.Format("{0}/{1}/{2}", pc.GetYear(d), pc.GetMonth(d), pc.GetDayOfMonth(d)));
DateTime date = new DateTime(2013, 10, 24);
var calendar = new PersianCalendar();
var persianDate = new DateTime(calendar.GetYear(date), calendar.GetMonth(date), calendar.GetDayOfMonth(date));
var result = persianDate.ToString("yyyy MMM ddd", CultureInfo.GetCultureInfo("fa-Ir"));
You can use PersianDateTime:
PM> Install-Package PersianDateTime
The Reference: PersianDateTime
You can use string.Split() if you need customization.
Adding up to other answers, You can get the first one by using PersianDateTime:
var gregorianDate = "Thursday, October 24, 2013";
var date = DateTime.Parse(gregorianDate);
var persianDate = new PersianDateTime(date);
var result = persianDate.ToString("dddd d MMMM yyyy");
The fowlling code should work in .net 4+ :
DateTime date=Convert.ToDateTime("2020-07-12T19:30:00.000Z");
string persianDateString = date.ToString("yyyy/MM/dd",new CultureInfo("fa-IR"));
persianDateString value would be:
1399/04/23
In windows 10, using framework 4+:
Console.WriteLine(Convert.ToDateTime("1392/08/02",new CultureInfo("fa-IR")));
or
Console.WriteLine(DateTime.Parse("1392/08/02", new CultureInfo("fa-IR")));
You can convert a DateTime to Iran time using this method:
DateTime timeInIran = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTimeToConvert, "Iran Standard Time" );

Categories