Convert a string into Date [duplicate] - c#

This question already has answers here:
How to convert a date string "dd/MM/yyyy" format into "MM/dd/yyyy" in asp.net c#?
(4 answers)
Closed 6 years ago.
I have a string containing information 08/23/2016~08:00 - 12:00~D . I want to first convert it into date i.e 08/23/2016 and then convert this into Tuesday,August 23,2016 . Is it possible to convert it in this format?

A combination of string- and DateTime-methods
string input = "08/23/2016~08:00 - 12:00~D";
string datePart = input.Split('~')[0].Trim();
DateTime dt;
if (DateTime.TryParse(datePart, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out dt))
{
string output = dt.ToString("dddd,MMMM dd,yyyy", DateTimeFormatInfo.InvariantInfo);
}
Custom Date and Time Format Strings

Try this way
string inp = "08/23/2016~08:00 - 12:00~D";
string str = inp.Split('-')[0].Trim();
DateTime date = DateTime.ParseExact(str, "MM/dd/yyyy~hh:mm", CultureInfo.InvariantCulture);
string s1 = date.ToString("MM/dd/yyyy");
string s2 = date.ToString("dddd,MMMM dd,yyyy");

Related

Convert string to DateTime with format "dd/MM/yyyy" [duplicate]

This question already has answers here:
Convert string to DateTime in c#
(3 answers)
Closed 6 years ago.
I have a string that is a date in the format of "MMddyyyy". I need to convert it to a DateTime with formatting "dd/MM/yyyy". I created the following code, but I keep getting a Format Exception. Parse, ParseExact, TryParseExact all give a Format Exception also.
string dater = "10312016";
DateTime condate = Convert.ToDateTime(dater);
I have tried the following:
string dater = "10312016";
DateTime condate = DateTime.ParseExact(dater, "MMddyyyy", CultureInfo.InvariantCulture);
DateTime condate2 = DateTime.ParseExact(dater, "yyyyMMdd", CultureInfo.InvariantCulture);
condate works, but it is in the wrong format. It needs to be "dd/MM/yyyy"
string dater = "10312016";
DateTime condate = Convert.ToDateTime(dater).tostring("dd/MM/yyyy");
Just try this. I am posting answer from my mobile. So tostring syntax may not right. When u type tostring in IDE then it will be alright.
Edit
sorry for later answer. see this code and it is working.
string dater = "10312016";
DateTime condate = DateTime.ParseExact(dater, "MMddyyyy", System.Globalization.CultureInfo.InvariantCulture);
string strDate = condate.ToString("dd/MM/yyyy");
Th
anks

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);

how to display only date from DateTime [duplicate]

This question already has answers here:
Display only date and no time
(11 answers)
Closed 8 years ago.
i m getting date from sql server database and it is displaying datetime as 1/15/2015 12:00:00 AM.
code in c# i used is:
dob_lbl.Text = reader[6].ToString();
//this is extracted using Sqlconnection so it's in array format.
i need only date to display.pls help.
try this:
string strDate = reader[6].ToString();
dob_lbl.Text = DateTime.ParseExact(strDate, "M/dd/yyyy hh:mm:ss tt",
CultureInfo.InvariantCulture).ToString("yyyy/MM/dd");
The ToShortDateString method of DateTime should help with this.
Use it like:
string strDate = reader[6].ToString();
DateTime dateTime = DateTime.Parse(strDate);
string justDateStr = dateTime.ToShortDateString();
You could simply split the string and use the first part of the resulting array.
Something like:
dob_lbl.Text = reader[6].ToString().Split()[0];

Sort a dateTime list in C# [duplicate]

This question already has answers here:
String was not recognized as a valid DateTime " format dd/MM/yyyy"
(13 answers)
Closed 8 years ago.
I want to sort a list of strings which are formatted to be parsed to dateTime
for example :
19-06-2014
18-06-2014
17-06-2014
// all are strings
with this block of code :
var orderedList = newlist.OrderByDescending( x => DateTime.Parse(x)).ToList();
this gives me the following exception : String was not recognized as a
valid DateTime.
As far as i know XX-XX-XXX is correct if you want to parse it to dateTime ?
Use DateTime.ParseExact or TryParseExact as dd-MM-yyyy is not a standard date format in most Cultures (where it's usually dd/MM/yyyy or MM/dd/yyyy).
IEnumerable<DateTime> dtes =
datesAsString.Select(
str => DateTime.ParseExact( str, "dd-MM-yyyy", CultureInfo.InvariantCulture )
).
OrderByDescending( dt = > dt );

changing date format from dd/MM/yyyy to MM/dd/yyyy [duplicate]

This question already has answers here:
How to change date format from DD/MM/YYYY or MM/DD/YYYY to YYYY-MM-DD?
(4 answers)
Closed 7 years ago.
I have a datepicker which shows date in format dd/MM/yyyy(i know i coould change there itself but by client want it that way) and in database its in format MM/dd/yyyy so i do want to convert in that way.
e.g. in text box 23/09/2010 and in c sharp its convert to mm/dd/yyyy(txtbo1.text)
Regards
Indranil.
If you really store the date as a string in the DB, you can convert the string format in the following manner:
DateTime.ParseExact(dateTimeString, "dd/MM/yyyy", null).ToString("MM/dd/yyyy")
If you are storing dates in a database, you should be storing them in a field with an appropriate data type. The format shouldn't come into it. You should parse your dd/MM/yyyy text into a DateTime variable, and pass it in a parameters of a date/time type to your database.
Use regex:
public static String DMYToMDY(String input)
{
return Regex.Replace(input,
#"\b(?<day>\d{1,2})/(?<month>\d{1,2})/(?<year>\d{2,4})\b",
"${month}/${day}/${year}");
}
using System.Globalization;
string dt = DateTime.Parse(txtDate.Text.Trim()).ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);
also can be done with this
public string FormatPostingDate(string str)
{
if (str != null && str != string.Empty)
{
DateTime postingDate = Convert.ToDateTime(str);
return string.Format("{0:MM/dd/yyyy}", postingDate);
}
return string.Empty;
}

Categories