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");
Related
This question already has answers here:
DateTime.Value.ToString(format) gives me 12 hour clock
(4 answers)
Closed 6 years ago.
I'm working with a project and one of its functional requirement is to create a datetime range in which I will select a set of data based on that.
This range should take today's datetime starting from 8:00:00 to 18:00:00, and then I want to convert the format to be yyyy-MM-ddThh:mm:ssZ, so I'm doing the following to approach that:
DateTime fromTime = DateTime.Now;
TimeSpan ts = new TimeSpan(8,0,0);
fromTime = fromTime.Date + ts;
string fromTimeFormat = fromTime.ToString("yyyy-MM-ddThh:mm:ssZ");
DateTime toTime = DateTime.Now;
TimeSpan tss = new TimeSpan(18, 0, 0);
toTime = toTime.Date + tss;
string toTimeFormat = toTime.ToString("yyyy-MM-ddThh:mm:ssZ");
The problem is that, the toTimeFormat is being converted to the 12h system, so when I use it later it's being considered as 6:00 AM.
Any ideas please?
Because you are using hh specifier which is for 12-hour clock format.
You need to use HH specifier which is for 24-hour clock format.
string toTimeFormat = toTime.ToString("yyyy-MM-ddTHH:mm:ssZ");
And you can simplify your code as;
string fromTimeFormat = DateTime.Today.AddHours(6).ToString("yyyy-MM-ddThh:mm:ssZ");
string toTimeFormat = DateTime.Today.AddHours(18).ToString("yyyy-MM-ddTHH:mm:ssZ");
This question already has answers here:
Convert UTC/GMT time to local time
(12 answers)
Closed 6 years ago.
I am trying to parse a string "20160918000500 +0200" to DateTime containing offset value "+0200".
I tried the following but it gives invalid DateTime exception.
DateTime dtDateTime = DateTime.Parse("20160918000500 +0200",new CultureInfo("yyyyMMddHHmmss zzz"));
Is there a way to convert the String exactly to Datetime with UTC offset value?
To preserve your Offset, use the DateTimeOffset.ParseExact method:
string str = "20160918000500 +0200";
var result = DateTimeOffset.ParseExact(str, "yyyyMMddHHmmss zzz", CultureInfo.InvariantCulture);
Console.WriteLine(result);
I would suggest to try out one of ParseExact methods of DateTime class
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
This question already has answers here:
How do I get the time difference between two DateTime objects using C#?
(9 answers)
Closed 7 years ago.
I have two dates of the form:
Start Date: 2007-03-24
End Date: 2009-06-26
Now I need to find the difference between these two in the following form:
2 years, 3 months and 2 days
How can I do this in c# windows form?
You need to use TimeSpan to get difference..
class Program
{
static void Main(string[] args)
{
string StartDate = "2007-03-24";
string EndDate = "2009-06-26";
System.DateTime firstDate = DateTime.ParseExact(StartDate, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
System.DateTime secondDate = DateTime.ParseExact(EndDate, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
System.TimeSpan diff = secondDate.Subtract(firstDate);
var totalDays = (diff).TotalDays;
var totalYears = Math.Truncate(totalDays / 365);
var totalMonths = Math.Truncate((totalDays % 365) / 30);
var remainingDays = Math.Truncate((totalDays % 365) % 30);
Console.WriteLine("Estimated duration is {0} year(s), {1} month(s) and {2} day(s)", totalYears, totalMonths, remainingDays);
Console.ReadLine();
}
}
.net already provide TimeSpan class to show differences between two datetime value.some properties of TimeSpan class show years,month and day interval seperately
This question already has answers here:
Display only date and no time
(11 answers)
Closed 8 years ago.
i m getting date from sql server database and it is displaying datetime as 1/15/2015 12:00:00 AM.
code in c# i used is:
dob_lbl.Text = reader[6].ToString();
//this is extracted using Sqlconnection so it's in array format.
i need only date to display.pls help.
try this:
string strDate = reader[6].ToString();
dob_lbl.Text = DateTime.ParseExact(strDate, "M/dd/yyyy hh:mm:ss tt",
CultureInfo.InvariantCulture).ToString("yyyy/MM/dd");
The ToShortDateString method of DateTime should help with this.
Use it like:
string strDate = reader[6].ToString();
DateTime dateTime = DateTime.Parse(strDate);
string justDateStr = dateTime.ToShortDateString();
You could simply split the string and use the first part of the resulting array.
Something like:
dob_lbl.Text = reader[6].ToString().Split()[0];