Display datetime into MM/dd/yyyy HH:mm format c# - c#

In database datetime is being stored in MM-dd-yyyy HH:mm:ss fromat.
However, I want to display datetime in "MM/dd/yyyy HH:mm" format.
I tried it by using String.Format().
txtCampaignStartDate.Text = String.Format("{0:MM/dd/yyyy
HH:mm}",appCampaignModel.CampaignStartDateTime);
Here appCampaignModel.CampaignStartDateTime is DateTime object having value in "MM-dd-yyyy HH:mm" format.
I want to display in "MM/dd/yyyy HH:mm" format.
Can anyone help me for this?

The slashes in this format MM/dd/yyyy HH:mm mean: "replace me with the actual separator of the current culture". You have to use CultureInfo.InvariantCulture explicitely:
txtCampaignStartDate.Text = appCampaignModel.CampaignStartDateTime.ToString("MM/dd/yyyy HH:mm", CultureInfo.InvariantCulture);
The "/" Custom Format Specifier
In database datetime is being stored in MM-dd-yyyy HH:mm:ss fromat.
Don't store datetimes with a format which means that you store them as (n)varchar. Use datetime instead.

txtCampaignStartDate.Text = appCampaignModel.CampaignStartDateTime
.ToString().Replace("-","/");
or
String.Format("{0:MM/dd/yyyy HH:mm}", dt);
Check : String Format for DateTime [C#]
or
String date = dt.ToString("MM/dd/yyyy HH:mm", DateTimeFormatInfo.InvariantInfo);
DateTime.ToString Method (String, IFormatProvider)

Why not just a simple replace?
txtCampaignStartDate.Text = BadString.Replace("-","/");

try using an added culture info:
CultureInfo ci = new CultureInfo("en-US");
txtCampaignStartDate.Text =
appCampaignModel.CampaignStartDateTime.ToString("MM/dd/yyyy HH:mm", ci);
p.s. the culture info used is an example.

To display your date in format you specified:
txtCampaignStartDate.Text = appCampaignModel.CampaignStartDateTime.ToString("g",DateTimeFormatInfo.InvariantInfo)
And for more date time formats you can go through this link.

Since you already have a DateTime object, just ToString it how you want:
appCampaignModel.CampaignStartDateTime.ToString("MM/dd/yyyy HH:mm");

Related

Change DateTime Format {"MM/dd/yyyy"} to {"dd/MM/yyyy"} and time should remain same in Unity

I want to change DateTime now to the Format {"MM/dd/yyyy"} using this code.
string.Format("{0:MM:dd:yyyy}", DateTime.Now)
and saving it.
after getting saved string I get DateTime in format {"MM/dd/yyyy"} . Now I want to convert it in another format so I can Parse to DateTime. when I try to parse MM/dd/yyyy to DateTime got an error
"FormatException: String was not recognized as a valid DateTime."
Thanks in Advance.
You can rather use DateTime.ParseExact which allows you to specify the exact date format you are expeting the input to have.
For example
var now = DateTime.Now;
Debug.Log(now.ToString("dd.MM.yyyy"));
var example1 = now.ToString("MM/dd/yyyy");
Debug.Log(example1);
var readTime1 = DateTime.ParseExact(example1, "MM/dd/yyyy", CultureInfo.InvariantCulture);
Debug.Log(readTime1.ToString("dd.MM.yyyy"));
var example2 = now.ToString("dd/MM/yyyy");
Debug.Log(example2);
var readTime2 = DateTime.ParseExact(example2, "dd/MM/yyyy", CultureInfo.InvariantCulture);
Debug.Log(readTime2.ToString("dd.MM.yyyy"));
See Fiddle
The format is only relevant for display
If you save it in a C# DateTime variable, there is no "format" when saving it, this DateTime is a struct data type which is universal and not bound to any specific format
If you want to use a specific format for parsing, you can use:
// Parse date and time with custom specifier.
CultureInfo provider = CultureInfo.InvariantCulture;
dateString = "Sun 15 Jun 2008 8:30 AM -06:00";
format = "ddd dd MMM yyyy h:mm tt zzz";
DateTime myDate = DateTime.ParseExact(dateString, format, provider);
You can use CultureInfo to optimize your format for you needs
If there is a need to save it as "MM/dd/yyyy", your should save it as string
best regards

DateTime.ParseExact not working in C#, Date format Conversion not working

Parse Time not working as I want to convert "13-06-2019 00:00:00"(dd-MM-yyyy HH:mm:ss) to "06-13-2019 00:00:00"(MM-dd-yyyy HH:mm:ss)
tried with Convert.toDateTime() and DateTime.ParseExact()
IFormatProvider culture = new CultureInfo("en-US");
var a = DateTime.ParseExact(a, "MM-dd-yyyy hh:mm:ss", CultureInfo.InvariantCulture);
var b = DateTime.ParseExact(a, "yyyy-MM-dd HH:mm:ss", null);
var c = DateTime.ParseExact(a, "yyyy-MM-dd HH:mm:ss", culture);
Nothing working in it
DateTime structure uses Gregorian calendar under the hood and there is no 13th month in that calendar.
So, parsing 13 with MM specifier is wrong. I strongly suspect that you try to use dd-MM-yyyy HH:mm:ss format instead.
string a = "13-06-2019 00:00:00";
DateTime b = DateTime.ParseExact(a, "dd-MM-yyyy HH:mm:ss", CultureInfo.InvariantCulture);
Here a demonstration
Your second and third examples also don't work since their formats are completely different than your string. When you parse your string with ParseExact method, your strings and your format should match exactly.
Also I want to mention that, both hh and HH specifiers would work in my code example. But as a general format consideration, using dd-MM-yyyy HH:mm:ss format is much more common and reliable than the other option.
The format parameter of DateTime.ParseExact(date,format,culture) is the source format of the date string to be converted and the return value is type date which you can convert back to string as per the desired format.
var a = "13-06-2019 00:00:00";
IFormatProvider culture = new CultureInfo("en-US");
DateTime b = DateTime.ParseExact(a, "dd-MM-yyyy hh:mm:ss", CultureInfo.InvariantCulture);
Console.WriteLine($"{b:MM-dd-yyyy HH:mm:ss}");

Converting string to date time

I have a date which comes in a string like so:
09/25/2014 09:18:24
I need it like this (yyyy-mm-dd):
2014-09-25 09:18:24
The object that this date goes into is a nullable date.
Tried this does not work:
DateTime formattedDate;
bool result = DateTime.TryParseExact(modifiedDate, "yyyy-MM-dd",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out formattedDate);
Any clues?
Thanks in advance.
From DateTime.TryParseExact
Converts the specified string representation of a date and time to its
DateTime equivalent. The format of the string representation must
match a specified format exactly.
In your case, they are not. Use yyyy-MM-dd HH:mm:ss format instead.
string s = "2014-09-25 09:18:24";
DateTime dt;
if(DateTime.TryParseExact(s, "yyyy-MM-dd HH:mm:ss",
CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
Console.WriteLine(dt);
}
It is a little bit unclear but if your string is 09/25/2014 09:18:24, then you can use MM/dd/yyyy HH:mm:ss format instead. Just a tip, "/" custom format specifier has a special meaning as replace me with current culture or supplied culture date separator. That means, if your CurrentCulture or supplied culture's DateSeparator is not /, your parsing operation will fail even if your format and string matches exactly.
If you have already a DateTime and you want to format it, you can use DateTime.ToString(string) method like;
dt.ToString("yyyy-mm-dd", CultureInfo.InvariantCulture); // 2014-09-25
or
dt.ToString("yyyy-mm-dd HH:mm:ss", CultureInfo.InvariantCulture); // 2014-09-25 09:18:24
Remember, a DateTime does not have any implicit format. It just contains date and time values. String representations of them have formats.
In answer to your question, to convert it as you prefer, do it like this:
string originalDate = "09/25/2014 09:18:24";
DateTime formattedDate;
if (DateTime.TryParseExact(originalDate, "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out formattedDate))
{
string output = formattedDate.ToString("yyyy-mm-dd HH:mm:ss", CultureInfo.InvariantCulture);
}
And then output will have your desired format.
DateTime dateOf = Convert.ToDateTime("09/25/2014 09:18:24");
string myFormat = "yyyy-mm-dd";
string myDate = dateOf.ToString(myFormat); // output 2014-18-25
Datetime format

C# DateTime Conversion from yyyy-MM-ddTHH:mm:ss to dd MMM yyyy

How to convert "yyyy-MM-ddTHH:mm:ss" into "dd MMM yyyy" format? For Instance, i want to convert 2013-04-16 05:30:05 into 16 April 2013. What is the correct method to achieve this?
First ParseExact then do ToString (I assume that you have string object, if you have DateTime object, skip first line)
var dateTime = DateTime.ParseExact(yourDateString, "yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture);
var yourNewString = dateTime.ToString("dd MMM yyyy");
Note that representation of DateTime you see in debugger is dependant on your current culture.
First, a DateTime has no format. But if you've already a string that represents a DateTime with the format yyyy-MM-ddTHH:mm:ss and you want to convert it to a string-date with format dd MMM yyyy you need to parse it to DateTime first.
Therefore use DateTime.ParseExact:
DateTime dt = DateTime.ParseExact("2013-04-16 05:30:05", "yyyy-MM-dd HH:mm:ss", null);
Now you can use DateTime.ToString:
string result = dt.ToString("dd MMM yyyy");
Note that you need to pass a different CultureInfo object to ParseExact/ToString if you want to parse with another DateTimeFormat than your current (f.e. force english month names instead of german: dt.ToString("dd MMM yyyy", CultureInfo.InvariantCulture)).
As others mentioned, a DateTime has no format. To parse a string literal to a Date you need to call DateTime.Parse (if the string is in a culture-specific format) or DateTime.ParseExact if you need to pass a format string.
The format can be a custom format like yyyy-MM-dd HH:mm:ss or one of the standard format strings, eg. s for yyyy-MM-ddTHH:mm:ss.
2013-04-16 05:30:05 it not in one of the standard formats, so you have to parse by passing a custom format string:
var dt = DateTime.ParseExact("2013-04-16 05:30:05", "yyyy-MM-dd HH:mm:ss", null);
On the other hand, yyyy-MM-ddTHH:mm:ss is the s standard format so you can just write:
var dt = DateTime.ParseExact("2013-04-16T05:30:05", "s", null);

Convert timestamp string to DateTime object in c#

I have timestamp strings in the following format 5/1/2012 3:38:27 PM. How do I convert it to a DateTime object in c#
var date = DateTime.ParseExact("5/1/2012 3:38:27 PM",
"M/d/yyyy h:mm:ss tt",
CultureInfo.InvariantCulture);
You input string looks like in en-us format, which is M/d/yyyy h/mm/ss tt. You have to use proper CultureInfo instance while parsing:
var ci = System.Globalization.CultureInfo.GetCultureInfo("en-us");
var value = DateTime.Parse("5/1/2012 3:38:27 PM", ci);
or
var ci = new System.Globalization.CultureInfo("en-us");
Try to use DateTime.ParseExact method like;
string s = "5/1/2012 3:38:27 PM";
DateTime date = DateTime.ParseExact(s, "M/d/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);
Console.WriteLine(date);
Converts the specified string representation of a date and time to its
DateTime equivalent using the specified format and culture-specific
format information. The format of the string representation must match
the specified format exactly.
Output will be;
01.05.2012 15:38:27
Be aware, this output can change based which Culture you used. Since my Culture is tr-TR, the date operator is . our culture.
Here is a DEMO.
Try the DateTime.ParseExact method
http://www.codeproject.com/Articles/14743/Easy-String-to-DateTime-DateTime-to-String-and-For
this maybe helps you. There you can find a detailled explanation of the ParseExact parameters.

Categories

Resources