This question already has answers here:
How to get the first five character of a String
(22 answers)
Closed 5 years ago.
I've a datetime:
DateTime dt = new DateTime(2003, 5, 1);
dt.DayOfWeek // returns Thursday
How I can split only first three characters from DayOfWeek e.g. Thu?
If you mean for it to work in different cultures, then your best bet is probably:
var abbr = culture.DateTimeFormat.GetAbbreviatedDayName(dayOfWeek);
where culture is a CultureInfo, for example:
var culture = CultureInfo.CurrentCulture;
In English cultures, this is the 3-letter version:
Sun
Mon
Tue
Wed
Thu
Fri
Sat
Marcs approach is the best here. But if i understand your question in a more general way, how to get the first three letters of an enum-value, you can use string methods if you use enum.ToString:
DateTime dt = new DateTime(2003, 5, 1);
string dow = dt.DayOfWeek.ToString();
dow = dow.Length > 3 ? dow.Remove(3) : dow;
Related
This question already has answers here:
Format date in C#
(7 answers)
How to format a date in C# by example?
(5 answers)
Closed 1 year ago.
I have 31 juli 2021 (norwegian date formatting), and adding 14 days to it, which gives me 14.08.2021.
I would like for the return date to be in the same format: 14 august 2021.
This is my current code:
int yearInt = Convert.ToInt32(year);
int monthInt = Convert.ToInt32(month);
int lastDayOfMonth = DateTime.DaysInMonth(yearInt, monthInt);
DateTime today = DateTime.Now;
DateTime invoiceDate = new(yearInt, monthInt, lastDayOfMonth);
DateTime invoiceDatePlus14 = invoiceDate.AddDays(14);
string rsvpBy_monthIsInt = invoiceDayPlus14.ToShortDateString();
I know how to get the month name off of the its corresponding int value:
static string getMonthName(int month) {
return CultureInfo.CreateSpecificCulture("no").DateTimeFormat.GetMonthName(month);
}
But how can I do the same to monthInt without re-factoring my whole approach?
If I need to - I need to, if so, how could I?
You may want to try this:
string rsvpBy_monthIsInt = invoiceDayPlus14.ToString("dd MMMM yyyy");
This question already has answers here:
How to find the first day of next month,if the current month is december
(12 answers)
Calculate difference between two dates (number of days)?
(17 answers)
Closed 5 years ago.
I would like to know how to calculate the remaining days of the month e.g (15 Feb 2017 to 28 Feb 2017) without the use of a datetimepicker set at 28 Feb 2017.
Here are my codes for subtraction of 2 datetimepicker:
DateTime startDate =
(DateTime)dateTimePicker2.Value;
DateTime endDate =
(DateTime)dateTimePicker1.Value;
TimeSpan ts = endDate.Subtract(startDate);
textBox10.Text = ts.Days.ToString();`
Here are the steps you need to go through:
take your current date
use DateTime.AddMonths() to generate a new date one month from your current date
create a new date that uses 1 for the day, and the month and year from the future date you just worked out
subtract your current date from the future date, this will give you a Timespan which contains the number of days difference
You can use the closely related question Calculate difference between two dates (number of days)? as a guide.
Here is an example, but you can use the Value property from your DateTimePicker instead. DateTime.DaysInMonth(int year, int month) is a helpful method.
DateTime beginDate = new DateTime(2017, 2, 15);
var daysLeft = DateTime.DaysInMonth(beginDate.Year, beginDate.Month) - beginDate.Day;
Console.WriteLine("days from Feb 15 to Feb 28: {0}", daysLeft);
Output:
days from Feb 15 to Feb 28: 13
You can try:
DateTime dt = DateTime.Now;
int days = dt.AddDays(1 - dt.Day).AddMonths(1).AddDays(-1).Day - dt.Day;
This question already has answers here:
Parse string to DateTime in C#
(9 answers)
Closed 7 years ago.
I have 3 separate strings with the following format:
01-29-2016: a date, picked from a bootstrap datepicker
1:00am start time, picked from a dropdown, format could also be e.g. 10:00pm
2:30am end time, picked from a dropdown, format could also be e.g. 11:30pm
Using the above strings I need to construct 2 DateTime properties that represent the time range, something like below:
2016-01-29 02:30:00
2016-01-29 01:00:00
I need the DateTime properties so I could update datetime database fields
You can combine both your time parts with date part respectively and use ParseExact method with MM-dd-yyyyH:mmtt format like;
var date = "01-29-2016";
var ts1 = "1:00am";
var ts2 = "2:30am";
var dt1 = DateTime.ParseExact(date + ts1, "MM-dd-yyyyH:mmtt", CultureInfo.InvariantCulture);
// 29.01.2016 01:00:00
var dt2 = DateTime.ParseExact(date + ts2, "MM-dd-yyyyH:mmtt", CultureInfo.InvariantCulture);
// 29.01.2016 02:30:00
Hi I have julian date string YYJJJ format. eg 05365(31st dec 2005). I want to covert to MMDDYY format(123105).
Is there any defined function for that in?
I faced same problem as I was try to convert dates from BACS 18 standard to a String. I couldn't find ready solution to this problem so I wrote this function:
private String bacsDateConvert(String bacsFormatDate)
{
int dateYear = Convert.ToInt16(bacsFormatDate.Substring(1, 2));
int dateDays = Convert.ToInt16(bacsFormatDate.Substring(3, 3));
DateTime outputDate = new DateTime();
outputDate = Convert.ToDateTime("31-12-1999");
outputDate = outputDate.AddYears(dateYear);
outputDate = outputDate.AddDays(dateDays);
String outputString = outputDate.ToString("yyyyMMdd");
return outputString;
}
//You may call it like this:
textBox4.Text = Convert.ToString(bacsDateConvert(bacsTxnValueDate));
You also may modify it slightly and easily make it return DateTime data type if you want to. I just needed to return a string in the above format.
First of all, there is no YY, JJJ and DD formats as a custom date and time format. One solution might be to split your string Year and DayOfYear part and create a DateTime with JulianCalendar class.
string s = "05365";
int year = Convert.ToInt32(s.Substring(0, 2));
// Get year part from your string
int dayofyear = Convert.ToInt32(s.Substring(2));
// Get day of years part from your string
DateTime dt = new DateTime(1999 + year, 12, 18, new JulianCalendar());
// Initialize a new DateTime one day before year value.
// Added 1999 to year part because it makes 5 AD as a year if we don't.
// In our case, it is 2004/12/31
dt = dt.AddDays(dayofyear);
// Since we have a last day of one year before, we can add dayofyear to get exact date
I initialized this new DateTime(.. part with 18th December because
From Julian Calendar
Consequently, the Julian calendar is currently 13 days behind the
Gregorian calendar; for instance, 1 January in the Julian calendar is
14 January in the Gregorian.
And you can format your dt like;
dt.ToString("MMddyy", CultureInfo.InvariantCulture) //123105
I honestly didn't like this way but this is the only one I can imagine as a solution.
This question already has answers here:
datetime.parse and making it work with a specific format
(2 answers)
Closed 9 years ago.
I have strings in the format
Mar 25 2013 6:30PM
and I want to convert them to datetime objects in the format
2013-03-25 18:30:00
how would I go about doing this?
Try this
DateTime dt
CultureInfo provider = CultureInfo.InvariantCulture;
System.Globalization.DateTimeStyles style = DateTimeStyles.None;
DateTime.TryParseExact("Mar 25 2013 6:30PM", "MMM d yyyy h:mtt", provider, style, out dt);
you can use:
var date = DateTime.Parse("Mar 25 2013 6:30PM");
Console.WriteLine(date.ToString()); /*This will output the date in the required format */