String date to DayOfWeek [duplicate] - c#

This question already has answers here:
Parse string to DateTime in C#
(9 answers)
Closed 8 years ago.
This code is a simplified version of what I'm trying to do:
this.dateValue = "8/12/2005";
return DateTime.dateValue.DayOfWeek.ToString();
I want to use my dateValue in stead of 'NOW'

Use DateTime.TryParseExact
this.dateValue = "8/12/2005";
DateTime dt;
// assuming the expected date is the 8th of Dec 2005, otherwise use m/d/yyyy
if (DateTime.TryParseExact(this.dateValue, "d/m/yyyy", CulterInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
return dt.DayOfWeek.ToString();
}
else
{
return null;
}

The DateTime.Now property returns a DateTime, DayOfWeek is a property of DateTime so you can simply do
return DateTime.Now.DayOfWeek.ToString();
To get the text version of the current Day of the Week
If you want to reverse this you can parse a new DateTime from the string.
return DateTime.Parse("8/12/2005").DayOfWeek.ToString();
Watch our for strings which can't be parsed. You may want to look into the TryParse method.

Related

How convert the string of "06/22/2019 00:00:00" to a valid DateTime type in format of 2019/06/22 without the part of hour and minute and second [duplicate]

This question already has answers here:
How to remove time portion of date in C# in DateTime object only?
(43 answers)
Closed 3 years ago.
I am doing an asp.net mvc project and I need to convert the string of "06/22/2019 00:00:00" to a valid DateTime type in format of 2019/06/22 without the part of hour and minute and second
You can use DateTime.ParseExact, here is an example :
http://net-informations.com/q/faq/stringdate.html
Finally, it should look like this :
string s = "06/22/2019 00:00:00";
DateTime myDate = DateTime.ParseExact(s, "MM/dd/yyyy HH:mm:ss",System.Globalization.CultureInfo.InvariantCulture);
Debug.WriteLine(myDate.ToString("MM/dd/yyyy"));
You can do this:
var dateString = "06/22/2019 00:00:00";
var datePart = dateString.Split(' ')[0];
var date = DateTime.Parse(datePart);
Though remember that DateTime will still have a default value for the time (12:00 AM), if you want the Date part only from the object, use date.Date which will return an instance with the default time (mentioned earlier).
DateTime contains default Time even if you access DateTime.Date. You can achieve format of date by converting Date into string.
Something like,
DateTime myDate = DateTime.ParseExact("06/22/2019 00:00:00", "MM/dd/yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
string dateInFormat = $"{myDate.Year}/{myDate.Month}/{myDate.Day}";
POC : .net Fiddle
You convert the string to a DateTime object and then to display just the date portion you can use ToShortDateString like this:
var myDateTime = DateTime.Parse( "06/22/2019 00:00:00") //presumably use a variable here instead.
var date = myDateTime.ToShortDateString();
How you want to display this can be done using the CultureInfo part as shown here: https://learn.microsoft.com/en-us/dotnet/api/system.datetime.toshortdatestring?view=netframework-4.8

How to convert date "2018-12-13T07:33:35.893Z" to 13/12/2018 [duplicate]

This question already has answers here:
How to create a .NET DateTime from ISO 8601 format
(7 answers)
Convert datetime without timezone
(4 answers)
Closed 4 years ago.
I have tried following way but doesn't work
dateTime="2018-12-13T07:33:35.893Z"
DateTime dt;
DateTime.TryParseExact(dateTime, out dt);
But I am always getting dt as {1/1/0001 12:00:00 AM}.
Can you please tell me why? and how can I convert that string to date?
I also tried Convert.ToDateTime but doesn't work.
What I actually want is getting the dd/MM/yyyy string'd DateTime so I could perform a query on a DB.
Have you got the original DateTime object or you simply have it in a string?
In case you've got it as DateTime:
string european = dateTime.ToString("dd/MM/yyyy");
In case you've got it as a string:
string date = "2018-12-13T07:33:35.893Z";
if(DateTime.TryParse(date , out DateTime result))
result.ToString("dd/MM/yyyy");
Have a look at the original MSDN documentation about the DateTime.ToString method
Since you've got a DateTime you can convert to that format:
var thisExactMoment = DateTime.Now;
thisExactMoment.ToString("dd/MM/yyyy");
With your "dateTime" variable, just perform dateTime.ToString("dd/MM/yyyy") and you're ready to go.
var dateTime = "2018-12-13T07:33:35.893Z";
var x = DateTime.Parse(dateTime).ToString(#"MM\/dd\/yyyy");

How to convert string to datetime that with million seconds? [duplicate]

This question already has answers here:
Converting a string to DateTime object
(5 answers)
Closed 7 years ago.
I have a string which value is "2016-01-07 20:43:01,803".
I'd like to convert it use DateTime.Parse method. it is failed.
How to convert to datetime with this type of string?
You could use the ParseExact method.
var input = "2016-01-07 20:43:01,803";
DateTime dt = DateTime.ParseExact(input, "yyyy-MM-dd HH:mm:ss,fff", CultureInfo.InvariantCulture);
Try to use DateTime.ParseExact with the right format. (the last phrase is very very important: right format)
DateTime dt = DateTime.ParseExact("2016-01-07 20:43:01,803", "yyyy-MM-dd HH:mm:ss,fff", null);

Converting DateTime [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to convert DateTime object to dd/mm/yyyy in C#?
I'm new to c# and was hoping someone could help me clean up some code.
I have the following method which converts a DateTime to a custom Event string e.g. 30th of Jan 2012 is converted to 201201 (ignores the day)
public ConvertToEventDate(DateTime date)
{
var year = date.Year.ToString();
var month = date.Month.ToString();
month = month.Length == 2 ? month : "0" + month;
return year + month;
}
I was wondering if there is a better way of doing this conversion.
I'd do this;
public string ConvertToEventDate(DateTime date)
{
return date.ToString("yyyyMM");
}
you can also put this into an Extension method like this;
public static class ExtenstionMethods
{
public static string ToEventDate(this DateTime date)
{
return date.ToString("yyyyMM");
}
}
and then call it ike this;
DateTime date = new DateTime(2012, 30, 1);
date.ToEventDate();
as opposed to this;
ConvertToEventDate(date);
public string ConvertToEventDate(DateTime date)
{
return date.ToString("yyyyMM", CultureInfo.InvariantCulture);
}
Have a look at the custom formatting strings docs
Others have suggested ToString("yyyyMM") which is basically there - but I would suggest you probably want to specify the invariant culture. Otherwise if the thread's current culture uses a non-Gregorian calendar, you could end up with a month/year you're not expecting. So I'd use:
string text = date.ToString("yyyyMM", CultureInfo.InvariantCulture);
See the documentation for custom date and time format strings for more information if you want to change the exact format later.
Try
return date.ToString("yyyyMM");

Convert a string to datetime [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How can I convert a string into datetime in .NET?
I have a string in the following format "15/03/2046". how can convert this string to a DateTime object?
My problem is when I do Convert.ToDateTime("15/03/2046") I get an exception.
when I do Convert.ToDateTime("03/03/2046") every thing works fine.
so I guess that I have to specify the format somehow while converting....
DateTime.Parse or its sister method DateTime.ParseExact.
Use DateTime.ParseExact to specify the format of the input string:
DateTime d = DateTime.ParseExact(
"15/03/2046",
"dd/MM/YYYY",
CultureInfo.InvariantCulture
);
More generic code, using extension method, and default value in case if it can't parse date
void Main()
{
var dt = "15/03/2046";
dt.ToDateTime("fr-FR", DateTime.Now).Dump();
}
public static class Extensions
{
public static DateTime ToDateTime(this string dateTime, string culture, DateTime defaultValue)
{
DateTime dt;
if (DateTime.TryParse(dateTime, System.Globalization.CultureInfo.CreateSpecificCulture(culture), System.Globalization.DateTimeStyles.None, out dt))
return dt;
else
return defaultValue;
}
}

Categories