How to set Datetimepicker to Month and Year only format? - c#

How can I set the Datetimepicker-Format to MM/YYYY?

Use DateTimerPicker.Format property. Check MSDN
public void SetMyCustomFormat()
{
// Set the Format type and the CustomFormat string.
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "MM/yyyy";
}

You can use DateTimerPicker.Format property as following:
DateTimerPicker.Format = DateTimePickerFormat.Custom;
DateTimerPicker.CustomFormat = "MMMM yyyy";
DateTimerPicker.ShowUpDown = true;
Note: DateTimerPicker.ShowUpDown = true; is for making the calendar not visible

This will give you some more options to display date with diff formats.
DateTimerPicker.Format = DateTimePickerFormat.Custom;
DateTimerPicker.CustomFormat = "MM/yyyy"; // this line gives you only the month and year.
DateTimerPicker.ShowUpDown = true;
Below are the different formats to display date alone on the date time picker control of the Windows applciation C#
DateTimerPicker.CustomFormat = "MMMM yyyy";
The above code will display the full month name and year like "August 2018"
DateTimerPicker.CustomFormat = "MMMM yyyy";
The above code will display the full month name, date and year like "08 August 2018"
DateTimerPicker.CustomFormat = "dd MM yyyy";
This line will give the date in the specified format with out forward slash "08 08 2018"
DateTimerPicker.CustomFormat = "dd/MM/yyyy";
This line will give more specific format to the date "08/08/2018"
DateTimerPicker.CustomFormat = "dd/MMM/yyyy";
This line give date in the format of "08/Aug/2018"

This line worked for me
DateTimePicker1.Value.ToString("MMMM dd yyyy")
output: August 2019

Related

How to Convert a datetime to Long Date?

Below is my code,
DateTime DateFrom_ = Convert.ToDateTime(CSO.dateFrom);
I am getting the value currently like - 29/10/2019 00:00:00
what i want is I want to convert above to this format - Tue, 29 Oct 2019
can anyone please guide me how to do this, Thank you.
Below one worked for me,
DateTime DateFrom_ = Convert.ToDateTime(CSO.dateFrom);
var convertedDate = DateFrom_.ToString("ddd', 'dd' 'MMM' 'yyyy");
You can do it easily, using DateTime.ParseExact to parse from certain format, and then use ToString with different format to represent it based on your need:
var date = DateTime.ParseExact("29/10/2019 00:00:00", "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
date.ToString("ddd, dd MMM yyyy"); // output Tue, 29 Oct 2019
Please note, if you need culture specific long date representation, then you should use ToString("D", cultureInfo) overload. That way, your format will be aligned with the culture defined format.
I think using DateTime.ToString() with the specific format should do the work. Try this one:
date.ToString("D", CultureInfo.CreateSpecificCulture("en-US"));
As it's been mentioned in here, you want to be using "DateTime.ParseExact"
var str = "Tue, 29 Oct 2019";
var date = DateTime.ParseExact(str, "ddd, dd MMM yyyy", CultureInfo.InvariantCulture);
You may split the date and use DateTime.ParseExact.
string[] tokens = "29/10/2019 00:00:00".Split(' ');
DateTime DateFrom_ = DateTime.ParseExact(tokens[0], "dd/MM/yyyy", CultureInfo.InvariantCulture);
var dateString = DateFrom_.ToString("ddd, dd MMM yyyy");

Unable to parse DateTime with a custom format

After reading some other similar questions and trying their suggestions, I'm still unable to get my time to parse into a DateTime-
string time1 = File.ReadAllText(#"C:\Reminders\Reminder1Time.txt");
DateTime dt1 = DateTime.ParseExact(time1, "hh:mm:tt", CultureInfo.InvariantCulture);
dateTimePicker1.Value = dt1;
time1 is a string value of 9:00 AM Other questions have mentioned to use ParseExact to specify a custom format, but it's still not parsing.
The error I get thrown is on the second line
String was not recognized as a valid DateTime.
How would I get the dateTimePicker1 to display the value from the time1 string?
Looks like a stray colon and an extra h if you are expecting a 12 hour clock 1-12 without a leading zero and the AM PM marker with whitespace.
Try: h:mm tt
All of the formatting options are buried in the documentation, here.
var datefromFile = File.ReadAllText(FILELOCATION);
var format = "dd-MM-yyy hh:mm:ss,fff";
var formattedDateTime = DateTime.ParseExact(dateFromFile, format, CultureInfo.InvariantCulture);
Make your format Explicit, remember dd for date capital MM for month and yyyy for year. hh for hour mm for minutes and ss for seconds.
MSDN Formats:
https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx

Date Time Picker Custom Format

this is my code:
dateTimePicker1.ShowUpDown = true;
dateTimePicker1.CustomFormat = "HH:MM";
dateTimePicker1.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
Hi I use Custom Format for my dateTimePicker until do not show second but its format hide AM/PM in dateTimePicker how can show AM/PM in this format
just add tt for AM/PM.
dateTimePicker1.CustomFormat = "HH:mm tt"
and it should be mm for minutes.
Custom Date and Time Format Strings

How to convert 31/03/2011 00:00:00 date format to 31 Mar 2011 in C#

I have got below date format in my result,I am using C# 2.0
ExpiryDate = "31/03/2011 00:00:00";
spnExpiryDate.InnerHtml = ExpiryDate;
Now when I am going to show this in HTML it should be converted to 31 Mar 2011 format.
Please suggest!
First, parse the string to a DateTime, and then format it using the DateTime's ToString:
DateTime myDateTime = DateTime.Parse( "31/03/2011 00:00:00" );
spnExpiryDate.InnerHtml = myDateTime.ToString("dd MMM yyyy");
This should do it:
var culture = System.Globalization.CultureInfo.CreateSpecificCulture("en-GB");
spnExpiryDate.InnerHtml = DateTime.Parse(ExpiryDate, culture).ToString("d MMM yyyy");
You can leave out the culture bits if your server's default culture is already dd/mm/yyyy.
First parse date then format how you need:
DateTime dt = DateTime.Parse("31/03/2011 00:00:00");
.InnerHtml = String.Format("{0:dd MMM yyyy}", dt);
There is good link that i use almost everytime when i need format date.
Also check this aswer about extention method for datetime formatting.
at first parse your string to DateTime
var date = DateTime.Parse("31/03/2011 00:00:00", "dd/mm/yyyy HH:mm:ss");
then user method "ToString"
date.ToString("dd MMM yyyy")
You didn't say how you are outputting it, but basically you want to provide the format when you call the ToString:
aTextBox.Text = myDate.ToString("dd MMMM yyyy");
The another way is
CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(DateTime.Now.Month);
gives you Month Name

date problem in C#

if i have this in a string : 10/13/2010 8:38:40 AM
how to make it to this: 13/10/2010 08:38:40
thank's in advance
DateTime.ParseExact("10/13/2010 8:38:40 AM","MM/dd/yyyy h:mm:ss tt",CultureInfo.InvariantCulture).ToString("dd/MM/yyyy HH:mm:ss")
edited to make sure 24 hour clock is used in output
Use DateTime.Parse() to convert to a true DateTime object and then use the DateTime.ToString() method to output to the format you desire (code example coming):
var dateTime = DateTime.Parse("10/13/2010 8:38:40 AM");
var formattedString = dateTime.ToString("dd/MM/yyyy HH:mm:ss);
Quick and dirty:
DateTime.Parse("10/13/2010 8:38:40 AM", new CultureInfo("en-US")).ToString(new CultureInfo("en-GB"));
Since I know that those formats are for those cultures. However, you can read more about datetime formatting at:
http://msdn.microsoft.com/en-us/library/zdtaw1bw.aspx
Standard formatting:
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx
Custom formatting:
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
Or for a more general solution, just pass a format string to DateTime.ToString('formatString'). For example, what you want is DateTime.ToString("dd/MM/yyyy HH:mm:ss"). This allows you to make any format you want.
Example:
DateTime exDT = DateTime.Now;
string exOut = exDT.toString("dd/MM/yyyy HH:mm:ss");
Here's a cheat sheet! You can use ":" where you want it
d Short Date
D Long Date
t Short Time
T Long Time
f Full date and time
F Full date and time (long)
g Default date and time
G Default date and time (long)
M Day / Month
r RFC1123 date
s Sortable date/time
u Universal time, local timezone
Y Month / Year
dd Day
ddd Short Day Name
dddd Full Day Name
hh 2 digit hour
HH 2 digit hour (24 hour)
mm 2 digit minute
MM Month
MMM Short Month name
MMMM Month name
ss seconds
tt AM/PM
yy 2 digit year
yyyy 4 digit year
var strfrom = "10/13/2010 8:38:40 AM";
DateTime dt = DateTime.Parse(strfrom, new CultureInfo("en-US"));
Console.WriteLine(dt.ToString(new CultureInfo("en-GB")));
var curDate = DateTime.Now.ToString() ;
string customDateFormat = Convert.ToDateTime(curDate).ToString("dd/MM/yyyy");
another variant, one one line:
DateTime.Parse("10/13/2010 8:38:40 AM", new CultureInfo("en-US")).ToString("dd/MM/yyyy HH:mm:ss");

Categories