When my application starts I have a datetimepicker for a start time and end time.
dvSubmittedDateBegin.Format = DateTimePickerFormat.Custom;
dvSubmittedDateBegin.CustomFormat = "MMM dd yyyy - hh mm tt";
Everything works. However I've been asked to have the start default default at 5AM.
I created a new datetime and assigned the dvSubmittedDateBegin.Value - dt;
However the new datetime I guess I have to specify every int?
DateTime dt = new DateTime(2015, 6, 24, 05, 00, 0);
What happens tomorrow when its 6/25? Not sure how to fix this.
How about like;
DateTime dt = DateTime.Today + TimeSpan.FromHours(5);
or more simple
DateTime dt = DateTime.Today.AddHours(5);
You will get the current date from midnight with DateTime.Today and you will add 5 hours to it and it will be 5 AM of the current day.
You can use the AddDays(), AddHours(), AddMinutes() etc. methods:
DateTime dt = DateTime.Today.AddHours(5);
You can create a function that would return a particular date where you pass all the components and define time components as default parameters:
DateTime CreateDateWith5amStart(int year, int month, int day, int hour = 5, int minute = 0, int second = 0)
{
return new DateTime(year, month, day, hour, minute, second)
}
If they provide only date components, it will set time to 5 a.m. If they need a different time, they can provide time components.
Related
I tried to insert into my MS Access database a specific format to see how just the "time" in my database behaves, but when I tried to show what I got the date also, but I want to insert just the time.
I tried to convert the datetime variable to specific format and insert that
DateTime starttime = Convert.ToDateTime(DateTime.Now.ToShortTimeString());
DateTime nstarttime = Convert.ToDateTime(starttime.ToString("HH:mm"));
06/04/2019 22:55:00 that what I got and I want just the time
without the date
Your main issue is, that in .Net the time part of a DateTime is not a DateTime but a TimeSpan:
DateTime dateTime = new DateTime(2019, 6, 4, 22, 55, 0);
TimeSpan timePart = dateTime.TimeOfDay;
In Access (VBA) however, a "time only" value is the time of the date of the VBA Date epoch which is 1899-12-30. Depending on how you insert the time in an Access table, you may have to apply the time part to the epoch to obtain a value like that Access would use:
DateTime dateTime = new DateTime(2019, 6, 4, 22, 55, 0);
TimeSpan timePart = dateTime.TimeOfDay;
DateTime vbaEpoch = new DateTime(1899, 12, 30);
DateTime vbaTime = vbaEpoch.AddTicks(timePart.Ticks);
Of course, when reading back the values, ignore the date part:
TimeSpan timeOfDay = vbaTime.TimeOfDay;
This has been answered so many times, but nevertheless here is the link to one of the discussions Link 1:
Please check also this Link 2 with may useful patterns
This detail is one of the actual examples provided on Link 1:
DateTime dt = DateTime.Parse("6/22/2009 07:00:00 AM");
dt.ToString("HH:mm"); // 07:00 // 24 hour clock // hour is always 2 digits
dt.ToString("hh:mm tt"); // 07:00 AM // 12 hour clock // hour is always 2 digits
dt.ToString("H:mm"); // 7:00 // 24 hour clock
dt.ToString("h:mm tt"); // 7:00 AM // 12 hour clock
Hope this helps!
Cheers and happy coding!
I am trying to create a DateTime object, but it seems to be giving me an error.
int month = "1"
int year = "2017"
DateTime date = new DateTime(year, month, DateTime.Day);
It doesn't seem to like DateTime.Day. It says an object reference is required for the non-static field.
How could I get today's day(16th) as a parameter? Also, I need the date to have hh:mm:sss... how could I do that?
Thanks for your help!
Use
var day = DateTime.Now.Day;
for today.
You can add hh:mm:sss to the date object in the constructor too:
DateTime date = new DateTime(year, month, DateTime.Now.Day, 10, 11, 12);
10 => hours
11 => minutes
12 => seconds
Of course you can use DateTime.Now.Hour etc. for the current values.
An ArgumentOutOfRangeException is thrown if the values are not valid for a real date, e.g. 30.2.xxxx.
You can print the date object in different formats, read the MS Documentation for all possibilities.
It should be:
int month = 1;
int year = 2017;
DateTime date = new DateTime(year, month, DateTime.Now.Day);
Take note, you declare integer without quotation marks:
int month = 1;
To convert it on 24 hour format with milliseconds as requested on comment:
string strResult = string.Format("{0:MM/dd/yyyy HH:mm:ss.fff}", date);
//Results: 02/17/2017 00:00:00.000
For 12 hour:
string strResult = string.Format("{0:MM/dd/yyyy hh:mm:ss.fff}", date);
//Results: 02/17/2017 12:00:00.000
I have a couple of DateTime startTime and endTime. I would like them to be in MMM yyyy format ("August 2017") but if I parse them ToString, i can't loop because, well, it's a string now, there is no AddMonths method. For exemple :
var formattedStartTime = startTime.ToString("MMMM yyyy");
var formattedEndTime = endTime.ToString("MMMM yyyy");
for (var date = formattedStartTime; date < formattedEndTime; date = date.AddMonths(1)) // nope
How can i parse my variables and loop through every month in between two dates ?
By calling ToString you are obviously converting your dates to a string, which know nothing about the original date they represent and as such also cannot perform any date related operations.
The solution is to simply convert to string only when you are actually displaying the object:
for (var date = startTime; date < endTime; date = date.AddMonths(1))
{
Console.WriteLine(date.ToString("MMM yyyy"));
}
Be careful with such date comparisons though, since depending on the actual days of the month and the time component in the startTime and endTime, you might skip or include a result you do not expect.
For example with startTime = new DateTime(2017, 1, 2) and endTime = new DateTime(2017, 2, 3) (February 3rd), you would get February in the result but with endTime = new DateTime(2017, 2, 1) (February 1st) you wouldn’t.
Every time that I create a non-nullable datetime in my mvc3 application it defaults to now(), where now is current date with current time. I would like to default it to today's date with 12am as the time.
I'm trying to default the time in my mvc...but...the following isn't setting to todays date #12am. Instead it defaults to now with current date and time.
private DateTime _Begin = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 12, 0, 0);
public DateTime Begin { get { return _Begin; } set { _Begin = value; } }
How can I set to 12am for the current date for non-nullable datetime?
You can use the Date property of the DateTime object - eg
DateTime midnight = DateTime.Now.Date;
So your code example becomes
private DateTime _Begin = DateTime.Now.Date;
public DateTime Begin { get { return _Begin; } set { _Begin = value; } }
PS. going back to your original code setting the hours to 12 will give you time of noon for the current day, so instead you could have used 0...
var now = DateTime.Now;
new DateTime(now.Year, now.Month, now.Day, 0, 0, 0);
I believe you are looking for DateTime.Today. The documentation states:
An object that is set to today's date, with the time component set to 00:00:00.
http://msdn.microsoft.com/en-us/library/system.datetime.today.aspx
Your code would be
DateTime _Begin = DateTime.Today;
Using some of the above recommendations, the following function and code is working for search a date range:
Set date with the time component set to 00:00:00
public static DateTime GetDateZeroTime(DateTime date)
{
return new DateTime(date.Year, date.Month, date.Day, 0, 0, 0);
}
Usage
var modifieddatebegin = Tools.Utilities.GetDateZeroTime(form.modifieddatebegin);
var modifieddateend = Tools.Utilities.GetDateZeroTime(form.modifieddateend.AddDays(1));
Only need to set it to
DateTime.Now.Date
Console.WriteLine(DateTime.Now.Date.ToString("yyyy-MM-dd HH:mm:ss"));
Console.Read();
It shows
"2017-04-08 00:00:00"
on my machine.
Related, so I thought I would post for others. If you want to find the UTC of the start of today (for your timezone) the following code works for any UTC offset (-23.5 thru +23.5). This looks like we add X hours then subtract X hours, but the important thing is the ".Date" after the add.
double utcOffset= 10.0; // Set to your UTC offset in hours (eg. Melbourne Australia)
var now = DateTime.UtcNow;
var startOfToday = now.AddHours(utcOffset - 24.0).Date;
startOfToday = startOfToday.AddHours(24.0 - utcOffset);
Most of the suggested solutions can cause a 1 day error depending on the time associated with each date. If you are looking for an integer number of calendar days between to dates, regardless of the time associated with each date, I have found that this works well:
return (dateOne.Value.Date - dateTwo.Value.Date).Days;
Try this:
DateTime Date = DateTime.Now.AddHours(-DateTime.Now.Hour).AddMinutes(-DateTime.Now.Minute)
.AddSeconds(-DateTime.Now.Second);
Output will be like:
07/29/2015 00:00:00
If I have a date and time in a DateTime object, can I remove say 10 minutes, or 24 hours etc from the date and time using a format string?
So if I have 1/1/1990 12:30:00pm and I wanted to remove 1 hour from it, can I use a format string?
edit
i need to store diary entries and the user can select a reminder type. so 1 hour before hand. so then i'd like to store the format string in a db that i can get and apply to a datetime to get the reminder date time
Something like this might do what you need. Not sure what you mean by subtracting an hour using a format string though.
DateTime dt = new DateTime(1990, 1, 1, 12, 30, 0);
string s = dt.Subtract(new TimeSpan(1, 0, 0)).ToString("MM/dd/yyyy hh:mm:ss tt")
Best way is to convert to a DateTime object and then back to the formatted string (if that's what you need):
var time = DateTime.Parse("1/1/1990 12:30:00pm");
time = time.AddMinutes(-10);
string timeString = time.ToString("MM/dd/yyyy hh:mm:ss tt");
You can use this in your query if you have..
DATE_SUB(date,INTERVAL expr unit)
Example:
YourDate = DATE_SUB(1/1/1990 12:30:00pm, INTERVAL 1 HOUR)
YourDate = DATE_SUB(1/1/1990 12:30:00pm, INTERVAL 12 MINUTE)
YourDate = DATE_SUB(1/1/1990 12:30:00pm, INTERVAL 45 SECONDS)