I want to return the name of day like Saturday or Monday
I used this code :
DateTime date = new DateTime(DateTime.Now.Date.Day);
MessageBox.Show(date.DayOfWeek.ToString());
But it doesn't work it return the name of day but doesn't correct day
and when i change the date in my computer it still return the same day
Rather try something like
MessageBox.Show(DateTime.Today.DayOfWeek.ToString());
DateTime.Today Property
Gets the current date.
Your problem is that
DateTime date = new DateTime(DateTime.Now.Date.Day);
evaluates to
{01/Jan/0001 12:00:00 AM}
The constructor you used was DateTime Constructor (Int64)
Initializes a new instance of the DateTime structure to a specified
number of ticks.
This line:
DateTime date = new DateTime(DateTime.Now.Date.Day)
Should be:
DateTime date = new DateTime(DateTime.Now)
You are putting the day in a date variable which will probably be in the year 1900.
Related
I have to calculate the date or create a date which is 10 days into next month. I have with me month and year. If the month is April and year is 2019 I need a date which is 10th of may 2019.
You can simply use the methods available in the DateTime structure to do math with dates
// As example, replaces it with whatever date you have
DateTime current = new DateTime(2019,4,5);
DateTime next = new DateTime(current.Year, current.Month, 1).AddMonths(1).AddDays(9);
You can use AddMonths() and AddDays() extension method of DateTime.
From MSDN :
AddMonths() : Returns a new DateTime that adds the specified number of
months to the value of this instance.
AddDays() : Returns a new DateTime that adds the specified number of
days to the value of this instance.
//Considered this is your Current date
DateTime existingDate = new DateTime(2019, 4, 1);
//Below code will add +1 month to current month and +9 days to current date.
var result = existingDate.AddMonths(1).AddDays(9);
Output :
CurrentDate : 4/1/2019 12:00:00 AM
Next Date (+1 month and +9 days) :5/10/2019 12:00:00 AM
POC : .net Fiddle
var dateNow = DateTime.Now.AddMonths(1);
var date = new DateTime(dateNow.Year, dateNow.Month, 10);
Try this:
DateTime dt = new DateTime(2019, 04, 01);
DateTime newDT = dt.AddMonths(1).AddDays(9);
I have searched online and i only managed to find codes to set the year month and day.
dateTimePicker2.Value = new DateTime(2017,12,31);
I tried using the custom format and it does not seem to work
dateTimePicker2.CustomFormat = "DD/MM";
dateTimePicker2.Value = new DateTime(12,31);
You cannot create DateTime object only from day and month. DateTime simply doesn't have this kind of constructor. DateTime Constructors
So you need to go with some kind of "workaround"
- Use "dummy" year and when you need to use a date - use only Month and Day properties.
var dummyYear = 2000;
dateTimePicker2.Value = new DateTime(dummyYear, 12, 31);
Another workaround will be to use ParseExact method which will create DateTime based on the format you are using "dd/MM"
var date = DateTime.ParseExact("31/12", "dd/MM", CultureInfo.InvariantCulture);
dateTimePicker2.Value = date; // 12/31/2017
Notice that when you did not provide a year - current year will be used.
Another notice: DD is invalid format for days it should be lower case "dd"
You cannot partially set the date without a year, it's not valid.
What you cand do is specify the month and date in code as "default" values, and get the current year programmtically (or whatever year you want), and use that value for the year.
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "dd/MM";
dateTimePicker1.Value = DateTime.Now;
I want convert long dateTime to only date in C#. and both in dateTime type
DateTime creationDate = DateTime.Now;
DateTime shortDate = creeationDate.Date;
A DateTime is a struct, which always has -- as the name suggests -- date and time components. You cannot remove the time and there is, as far as I know, no built-in date-only type in .NET (maybe there is in some 3rd party libraries).
You have already discovered the Date property of DateTime which returns an new DateTime with the time components set to 00:00:00.000.
If you only need to output your current date you must apply some formatting on the DateTime
DateTime creationDate = DateTime.Now;
string shortDate = creatationDate.ToString("yyyy-MM-dd"); //or whatever formatting you need.
If you need calculations on your date, but want to ignore the time component, you could initialize your date accordingly, as you have done in your code.
DateTime creationDate = DateTime.Now.Date; //sets time component to 00:00
DateTime otherDate = new DateTime(2016, 11, 2, 0,0,0); //also initializies with time 00:00
int diff = (int)(otherDate-creationDate).TotalDays; //number of days between the two dates
I want to insert just year : month : day for a specific column in a database table row, but my variable is of DateTime Type.
To remove milliseconds I just used following code:
DateTime createdate = System.DateTime.Now;
createdate = createdate.AddTicks(-(createdate.Ticks % TimeSpan.TicksPerSecond));
How to remove hours, minutes, seconds from this?
You can use .Date property which set's the time part to midnight.
DateTime justDate = createdate.Date;
or you can use DateTime.Today which generates the same result for DateTime.Now.Date value.
DateTime justDate = DateTime.Today;
If you use SQL Server, date type is mapped with DateTime on CLR side which you can safely insert that value.
Use the Date property on your createdate variable to return just the date component without the time component.
DateTime createdate = System.DateTime.Now;
createdate = createdate.AddTicks(-(createdate.Ticks % TimeSpan.TicksPerSecond));
var dateOnly = createdate.Date;
Note you will still have the time in the object, set to 00:00:00
Hi I currently have a TimePicker. It returns an object TimeSpan.
What I need to do is to set a DateTimeOffset that is equal to current date plus the TimeSpan from the TimePicker.
How can I actually get the current DateTimeOffset.now that doesn't have a Time on it, only the Date so that I can add the offset to it.
Thanks
As in DateTime object you have a Date property, it returns date part without time (it means time is 00:00:00).
DateTime today = DateTimeOffset.Now.Date;
DateTime result = today + yourTimeSpan;
With this solution will lost Offset information (because Date is a DateTime). To keep it you just need to subtract time part:
DateTimeOffset now = DateTimeOffset.Now;
DateTimeOffset result = now - now.Time + yourTimeSpan;
Or with constructor:
DateTimeOffset now = DateTimeOffset.Now;
DateTimeOffset result = new DateTimeOffset(now.Date + yourTimeSpan, now.Offset);
Can you not just .Date it?
var a = DateTimeOffset.Now.Date;
try using:
DateTime.Today
instead of Now.