How to Get Desire Time and Date Format in C#? - c#

I want to Get date-time Format like (30 April 1990) my date is store in sql server Database
in default Format mm/dd/yyyy.
how to do that?
tsddate.Text = orderReader["deliveryDate"] as DateTime = new DateTime(?,,????);

You can specify custom datetime formats using the ToString overload.
like:
DateTime now = DateTime.Now;
string formatted = now.ToString("dd-MM-yyyy");
And in your situation it would be something like:
DateTime date = DateTime.ParseExact(orderReader["deliveryDate"].ToString(), "MM/dd/yyyy HH:mm:ss", new System.Globalization.CultureInfo("en-US"));
tsddate.Text = date.ToString("dd MMMM yyyy");
see all the formats you can use here:
http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx

tsddate.Text = String.Format("{0:dd MMMM yyyy}",orderReader["deliveryDate"]);

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 format conversion mm/dd/yyyy to yyyy/mm/dd

Can someone please let me know how do I convert this datetime format into yyyyMMdd
2/28/2017 12:02:04 AM
At the output I should get 20170228
Any advice on this?
If you already have the DateTime as an object
string formattedDate = date.ToString("yyyyMMdd");
If you need to parse the value first.
string dateValue = "2/28/2017 12:02:04 AM";
string format = "M/d/yyyy hh:mm:ss tt";
DateTime dateTime = DateTime.ParseExact(dateValue, format,
System.Globalization.CultureInfo.InvariantCulture);
For reference you can find a breakdown of the Custom Date and Time Format Strings
You need to specify the format of the date.
If you want it for the current time you can try like this :
string dtime = DateTime.Now.ToString("yyyy/MM/dd");
This is the solution I have come up with for you:
string format = "M/d/yyyy hh:mm:ss tt";
string dateString = "2/28/2017 12:02:04 AM";
CultureInfo provider = CultureInfo.InvariantCulture;
DateTime date = DateTime.ParseExact(dateString, format, provider);
string output = date.ToString("yyyyMMdd");
If you're using C# 6 or later (VS2015), you can format DateTime objects easily by using string interpolation using a custom format string. The custom format string that you're looking for is "yyyyMMdd".
// create your preferred date and time in a new DateTime struct
DateTime yourDateTime = new DateTime(2017, 2, 28, 0, 2, 4);
// format yourDateTime as a string
string yourFormattedDateTime = $"{yourDateTime:yyyyMMdd}";
You can read more about interpolated strings at https://msdn.microsoft.com/en-us/library/dn961160.aspx, and, as previously mentioned by #Adam Carr, you can find more information on custom date and time format strings at https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx

How to convert string date in dateTime?

I have date in string format. I want to convert in date time in following format. I have referred few links but didn't get exact output.
String date = "03-23-16"; //MM-dd-yy
Requirement: it should be in date format like "March 23, 2016"
Can anybody suggest me how to convert this?
You could convert it to DateTime by using, say, DateTime.ParseExact and then convert it back to string using format "MMMM dd, yyyy":
String date = "03-23-16"; //MM-dd-yy note that MM here
DateTime dtInstance = DateTime.ParseExact(date, "MM-dd-yy", null); //this is how you convert string to DateTime
string newDate = dtInstance.ToString("MMMM dd, yyyy"); //this is how you convert it back with format as you want
Also, note that mm is minutes format in C# DateTime while MM is months.
DateTime.ParseExact("03-23-16", "MM-dd-yy", null).ToString("MMMM dd, yyyy")
You could convert it to Long Date Format by using ToLOngDateString()
string date = "03-05-16";
date = date.Replace('-', '/');
DateTime dt = Convert.ToDateTime(date);
string newDate = dt.ToLongDateString();
it will print :March, 03, 2016.
try this:
string date = "01-08-2008";
DateTime dt = DateTime.ParseExact("24/01/2013", "mm-dd-yy", CultureInfo.InvariantCulture);
parse to string:
dt.ToString("MMMM dd, yyyy");

DateTime convert in c#

I have DateTime variable in this format "dd/mm/yyyy hh:mm:ss".
I would like to convert this to "mm/dd/yyyy hh:mm:ss". (Month before day)
I tried to use DateTime.parse like this:
DateTime dt = new DateTime();
dt = dateTimePicker1.Value;
dt = DateTime.Parse(dt.ToString(), "mm/dd/yyyy", null);
but it wont work.
I also try it with DateTime.ParseExact() but still nothing happened.
Any suggestion?
A DateTime doesn't have any implicit format. It just has date and time values.
Formatting subject only applies when you try to get it's textual representation. If you wanna get string represetation of it, you can use .ToString() method like;
string s = dateTimePicker1.Value.ToString("MM/dd/yyyy hh:mm:ss",
CultureInfo.InvariantCulture);
And remember, mm specifier is for minutes, MM specifier is for months. Also hh specifier is for 12-hour clock and HH specifier is for 24-hour clock representations. You might wanna use HH instead.
I used InvariantCulture as a second parameter because / and : have special meaning as replace me current culture or supplied culture date or time separator.
string date = dateTimePicker1.Value.ToString("MM/dd/yyyy");
You can also use this below code for advance:
GregorianCalendar cal = new GregorianCalendar(); // or any type of calendar you want ...
string formatted = string.Format("{1:00}/{2:00}/{0} {3:00}:{4:00}:{5:00} ",
cal.GetYear(dt), cal.GetMonth(dt), cal.GetDayOfMonth(dt) , cal.GetHour(dt), cal.GetMinute(dt), cal.GetSecond(dt));
DateTime time = DateTime.Now;
string format = "MMM ddd d HH:mm yyyy";
Console.WriteLine(time.ToString(format));

Display datetime into MM/dd/yyyy HH:mm format 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");

Categories

Resources