How to get our own date time format in c# - c#

In my application , i have a control which is displaying current time in HH:mm:ss format. My question is if format is HH mm ss then, how this format is converted back to HH:mm:ss format
ex
03-08-2016 10:30:45 ==> my required format
03-08-2016 10 30 45 ==> current format. or it may be any other format
How can i get required format?...

DateTime class has a .ToString() method which accepts format string as it's argument. Please have a look at this
You can get your required format as follows:
var now = DateTime.Now;
var formattedDate = now.ToString("dd-MM-yyyy HH mm ss");

Let the dateInput be the date time object, and the dateFormatString be the required format. then you can use the .ToString() method to get the formatted date string; This can be achieved by using the following code:
string dateFormatString="dd-MM-yyyy HH:mm:ss";
string formattedDateString = dateInput.ToString(dateFormatString);

Suppose your current date time format dd-MM-yyyy HH mm ss like 03-08-2016 10 30 45 then you can convert your current format to dd-MM-yyyy HH:mm:ss as follow
var dt = DateTime.ParseExact("03-08-2016 10 30 45", "dd-MM-yyyy HH mm ss", CultureInfo.InvariantCulture);
string newdate = dt.ToString("dd-MM-yyyy HH:mm:ss");
using same mechanism you can convert any format to another

Let's say you can get the string value from your control that displays in MM-dd-yyyy HH mm ss format, you can use DateTime.ParseExact Method to convert it back to a DateTime variable.
var datetime = DateTime.ParseExact(controlDT.Text, "MM-dd-yyyy HH mm ss", CultureInfo.InvariantCulture);
var newDateTimeString = datetime.ToString("MM-dd-yyyy HH:mm:ss");

Easy way around. Try this:
DateTime date = DateTime.Now;
string dt = date.ToString("dd-MM-yyyy HH:mm:ss");
The output produced will be (as per your required format):
03-08-2016 05:25:47

Convert like this:
var date= DateTime.Now;
var Yourrequeired= date.ToString("dd-MM-yyyy HH: mm: ss");

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

How to Change Time In PM Or AM in 24 Hour format

how to Change Time According to PM OR AM please help me this in C#
string time = "0610"; OR "1200" OR "0100" OR "1430"
DateTime dt = DateTime.Parse(time);
i Need like this : 06:10 AM
You want to use ParseExact to parse the string into a date/time:
string time = "0610";
DateTime dt = DateTime.ParseExact(time, "HHmm"", CultureInfo.InvariantCulture);
// HH means 24-hour format with leading zero
Note that DateTime does not HAVE a format, it just represents a point in time. If you then want to display it with the AM/PM indicator, you could use ToString, or set the format specifier in the display control (if applicable):
string timeAmPm = dt.ToString("hh:mm tt");
Try this
//date is your datetime
DateTime date = DateTime.Now;
string formatTime = date.ToString("hh:mm tt", CultureInfo.InvariantCulture);
Console.WriteLine(formatTime);
See more:
https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings#ttSpecifier

String to DateTime in c#: 2018-05-10 10-AM

I would like to convert the String "2018-05-10 10-AM" to a DateTime-Object. I tried everything, including this (is throwing an Exception):
var date = DateTime.ParseExact("values[0]", "yyyy-MM-dd HH-tt",
System.Globalization.CultureInfo.InvariantCulture);
Anybody knows what I am doing wrong?
I think you should hh specifier instead of HH specifier since your hour part is 12-hour clock format, not 24-hour clock.
var date = DateTime.ParseExact("2018-05-10 10-AM", "yyyy-MM-dd hh-tt",
CultureInfo.InvariantCulture);
And also as David commented, you have a typo in your code. At least it should be like;
var date = DateTime.ParseExact(values[0], "yyyy-MM-dd hh-tt",
CultureInfo.InvariantCulture);
You can also use something like this:
var date = DateTime.ParseExact("2018-05-10 10-AM", #"yyyy-MM-dd hh-tt", new CultureInfo("en-US"));
and in your own example:
var date = DateTime.ParseExact(values[0], #"yyyy-MM-dd hh-tt", new CultureInfo("en-US"));
note that don't put variables in quotations.

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

How to Get Desire Time and Date Format in 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"]);

Categories

Resources