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")
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");
I need to calculate the annual use of a service starting from the date of signing. Something like:
select Count(*) from TABLENAME where Date >= MYDATE
MYDATE need to be calculate from a subscription date and I need to get the last year date from subscription referring to the current date
Some examples:
subscription date: 2007-06-29
if current date is : 2015-04-29 then date is: 2014-06-29
if current date is : 2015-06-29 then date is: 2015-06-29
if current date is : 2015-06-29 then date is: 2015-06-29
I'm using c# to calculate the date but it crashes in leapyear:
var date = new DateTime(DateTime.Now.Year, subscriptionDate.Month, subscriptionDate.Day);
if (DateTime.Now.Date < date)
{
date = date.AddYears(-1);
}
I was wondering if there were a clever/better way to do it in c# or mysql also handling leapyear
---UPDATE----
Running example with suggested solutions
Well, I'd do it in Noda Time, myself:
LocalDate subscriptionDate = ...;
LocalDate today = ...; // Need to take time zone into account
int years = Period.Between(subscriptionDate, today);
return subscription.PlusYears(years);
With .NET that would be slightly harder, but I'd still go for the approach of adding years (and letting it do the truncation for Feb 29th):
// Only call this *once* - otherwise you could get inconsistent results
DateTime today = DateTime.Today;
int years = today.Year - subscriptionDate.Year;
DateTime candidate = subscriptionDate.AddYears(years);
// We might have overshot, in which case lower the number of years.
return candidate <= today ? candidate : subscriptionDate.AddYears(years - 1);
Thanks to Yuri Dorokhov answer and Jon Skeet suggestion
I found a solution that works well and handle leap year:
int year = DateTime.Now.DayOfYear >= subscriptionDate.DayOfYear ?
DateTime.Now.Year : DateTime.Now.Year - 1;
var date = new DateTime(year, 1, 1).AddDays(subscriptionDate.DayOfYear - 1);
--------UPDATE------
I leave here this answer as reference but it does not handle well leap year so don't use it
Use mysql DATE_SUB function
DATE_SUB(Date, INTERVAL 1 YEAR)
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;
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 );