i don't know how to deal with this...
say currently, it's 2013/11/27, so it will be 2013/10 for previous month.
I searched there's something like (DateTime.Now.Month - 1), but how do I keep track of year please?
and say 2014 January, how do I turn it to 201312?
Thanks.
If I understand correctly, you can use DateTime.AddMonths method like;
DateTime now = DateTime.Now;
now = now.AddMonths(-1);
Console.WriteLine(now.ToString("yyyyMM"));
Output will be;
201310
Here a demonstration.
Use the DateTime.AddMonths method
Use AddMonth method of DateTime class to subtract a month:
var previousMoth = DateTime.Now.AddMonths(-1);
var year = previousMoth.Year;
var month = previousMoth.Month;
var day = previousMoth.Day;
Related
I got every part of date in the code that you can see below. But the problem is if we consider today's date I need day and month as 02 not as 2.
I need that 0 char in the beggining. How can I manage it?
DateTime dategift = DateTime.Now;
var year = dategift.Year.ToString();
var month = dategift.Month.ToString();
var day = dategift.Day.ToString();
var hour = dategift.Hour.ToString();
var min = dategift.Minute.ToString();
var sec = dategift.Second.ToString();
use the Zero placeholder
string day = dategift.Day.ToString("00");
or the "dd" custom format specifier
string day = dategift.ToString("dd");
If you're wanting to format the date, you can do so without pulling out all the various values as strings.
Something like:
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
Is there any way to get the day of the month as an int/string? E.G it's the 5th of June, I want the system to return 5 to me.
Easily you can get the day of month by returning
DateTime.Today.Day
Use the DateTime.Day property.
var dt = DateTime.Now;
var day = dt.Day;
sure,
as an integer, it's just DateTime.Today.Day
as a string it's DateTime.Today.Day.ToString("0")
I have a date like this:
equipBooking.BookedFromDteTme.Date = `{12/5/2013 12:00:00 AM}`
and I want to apply time like this, but I do not know how to get AM and PM from the end.
dtStartTimeHour.SelectedItem = equipBooking.BookedFromDteTme.TimeOfDay.Hours;
dtStartTimeMin.SelectedItem = equipBooking.BookedFromDteTme.TimeOfDay.Minutes;
**dtStartTimeAMPM.SelectedItem = equipBooking.BookedFromDteTme.???????.;**
Please help me.
I have tried something like this:
var startDatestr = equipBooking.BookedFromDteTme.TimeFrom.Split(new string[] { ":", ":",":",":" }, StringSplitOptions.RemoveEmptyEntries);
AM/PM = startDatestr[3]
If you just want the string you can do:
equipBooking.BookedFromDteTme.ToString("tt");
Or for a boolean result use:
bool isPM = (equipBooking.BookedFromDteTme.Hour >= 12);
BTW, you don't need to call TimeOfDay - you can get the Hour and Minute property directly from the DateTime:
dtStartTimeHour.SelectedItem = equipBooking.BookedFromDteTme.Hour;
dtStartTimeMin.SelectedItem = equipBooking.BookedFromDteTme.Minute;
dtStartTimeAMPM.SelectedItem = equipBooking.BookedFromDteTme.ToString("tt");
TimeSpan does not store time-of-day, but rather the length of any interval of time. It has no notion of AM/PM.
TimeOfDay returns the amount of time since midnight.
If Hours is more than or equal to 12, that will be PM.
try using dateTime.ToString("tt");
This is small code. But I couldn't find whats wrong with it. In my application I want to get the current month in long month format(ex:January). I used the following two lines of code.
DateTime now = DateTime.Now;
string month = now.Month.ToString("MMMM",CultureInfo.CurrentCulture);
but its return "MMMM" for the values of month. Can anybody tell me whats wrong in this code.
now.Month is the int representation the month e.g. 1 for January, that's why the .ToString("MMMM") works "strange".
What you need is to call the .ToString() directly on the DateTime object:
DateTime now = DateTime.Now;
string month = now.ToString("MMMM",CultureInfo.CurrentCulture);
String month =
System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(
DateTime.Now.Month);
How do you change the day part of a DateTime structure?
I have a DateTime with the value of 3/31/2009 8:00:00 AM and would like to change it to any other date but the time should be same. I don't need a solution to add day(s), I need to replace it with any arbitrary date.
How do I achieve this?
To construct a DateTime from two other DateTimes, one of which contains the date and one of which contains the time, you can use:
DateTime result = date.Date + time.TimeOfDay;
day = 1
month = 4
year = 2009
Date2 = new DateTime(day,month,year,Date1.Hours,Date1.Minute,Date1.Seconds)
s = s.AddDays(1)
You should use DateTime.AddDays method. Like this:
DateTime s2 = s.AddDays( 1 );