Hi all i want a datetime object. In which I want to set time part by my own.
Like i want DateTime object who will have todays system date and I want to set its time to 08:00:00.
Here you are:
DateTime structure
var myDate = DateTime.Today.AddHours(18);
Will give you a date value of The current date, at 6pm.
DateTime dt = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, 8, 0, 0);
That is the simplest I guess, in another case you can also create an extension method on DateTime probably to return you the above!
An extension method will also enable you to call it something like DateTime.MyToday() which will return you the current date with time set to 8:00:00 by default. So you need not do the entire new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, 8, 0, 0); every time you need this kind of date.
DateTime meetingAppt = new DateTime(
DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 8, 0, 0);
Related
I'm having strange behavior of DateTime in c#.
I'm trying to initialize a datepicker in January. So I make a new date:
DateTime MyDate = new DateTime(2017, 0, 15);
But I get this exception:
System.ArgumentOutOfRangeException: Year, Month, and Day parameters describe an un-representable DateTime.
If I use (2017, 1, 15) it works but the time dialog, initialized with:
DatePickerDialog dialog = new DatePickerDialog(
Activity,
this,
MyDate.Year,
MyDate.Month,
MyDate.Day);
Goes on February.
Well I tried to "cheat" and did:
DateTime MyDate = new DateTime(2017, 1, 15);
DateTime = DateTime.AddMonths(-1);
No error, but the date picker goes on February.
The only way to have January is:
DateTime MyDate = new DateTime(2017, 12, 15);
What am I doing wrong?
DateTime.Month is a value between 1 and 12, which aligns with what most people think a month 'number' is.
Per the android docs, theDatePickerDialog constructor you are calling accepts a zero-based month. It accepts values in the range 0-11, so you need to subtract one from the DateTime.Month.
DatePickerDialog dialog = new DatePickerDialog(Activity,
this, MyDate.Year, MyDate.Month - 1, MyDate.Day);
The issue is how the DateTime object treats month values (1-12) and how the DatePickerDialog treats month values (0-11).
DateTime constructor:
strange behavior of DateTime
DateTime MyDate = new DateTime(2017, 0, 15);
If we take a look at the DateTime constructor, it is clearly stated that the month value should be 1 through 12, which is not valid in your case and hence the exception. We can correct it as below:
DateTime MyDate = new DateTime(2017, 1, 15);
DatePickerDialog constructor:
Exception (or strange behavior) will arise when we try - new DatePickerDialog in combination with the month value of DateTime as the constructor of DatePickerDialog is expecting the month values from 0-11.
It is stated that int: the initially selected month (0-11 for compatibility with MONTH)
The approach which can then be followed is to give the correct index for month to DatePickerDialog constructor as below:
DateTime MyDate = new DateTime(2017, 1, 15);
DatePickerDialog dialog = new DatePickerDialog(Activity,
this,
MyDate.Year,
MyDate.Month - 1,
MyDate.Day);
How can I set certain DateTime value to tomorrow 9:00 AM
For example:
DateTime startTime = new DateTime.Now.AddDays(1).//setTime to 9:00 AM
Is there some SetDateTime value functionality that I don't know?
You can use two methods
DateTime.Today.AddDays(1).AddHours(9)
You can use this DateTime constructor like;
DateTime tomorrow = new DateTime(DateTime.Now.Year,
DateTime.Now.Month,
DateTime.Now.Day + 1,
9,
0,
0);
Console.WriteLine(tomorrow);
Output will be;
18.03.2014 09:00:00
As CompuChip mentioned, this throws exception if the current day is the last day of the month.
Better you can use DateTime.Today property with AddDays(1) and AddHours(9) because it get's to midnight of the current day. Like;
DateTime tomorrow = DateTime.Today.AddDays(1).AddHours(9);
DateTime dt=DateTime.Now;
DateTime Tomorrow = new DateTime(dt.Year,dt.Month,dt.Day+1,9,0,0);
How do I use a specific date as an input value?
var experiment1 = WorkingWithDates.GetDisplayString("London", DateTime.Today, 45.00);
Later on, I'll be convering it to a string, but I need it to be input as a DateTime.
public static string GetDisplayString(string city, DateTime date, double temp)
{
}
I'm using DateTime.Today as a placeholder, simply because it works. The thing is, I need it input as a specific day of a month of a year. (I've tried using (10, 10, 10) but it simply gives me a compiler error.
Edit.: I can't believe I did not figure out all I need to do is to add "new". Thanks folks
In (10, 10, 10) do you want the year 10?
Use the constructor that takes in a year, a month and a day. As below.
DateTime dt = new DateTime(2013, 12, 12);
then call your method
var experiment1 = WorkingWithDates.GetDisplayString("London", dt, 45.00);
PS: If you want year 10
DateTime dt = new DateTime(10, 10, 10);
will work. The year will be 0010
WorkingWithDates.GetDisplayString("London", DateTime.Today, 45.00);
you need a DateTime object to be used in place of DateTime.Today, so construct your instance using
DateTime newdate = new DateTime(2013,10,10) // year, month , day
and then
WorkingWithDates.GetDisplayString("London", newdate, 45.00);
I want to convert 18 digit string from LDAP AccountExpires to Normal Date Time Format.
129508380000000000 >> May 26 2011
I got the above conversion from using the following link.
http://www.chrisnowell.com/information_security_tools/date_converter/Windows_active_directory_date_converter.asp?pwdLastSet,%20accountExpires,%20lastLogonTimestamp,%20lastLogon,%20and%20badPasswordTime
I tried to convert by using DateTime.Parse or Convert.ToDateTime. But no success.
Anyone know how to convert it? Thanks very much.
Edited answer
It's the number of ticks since Jan-01-1601 in UTC, according to Reference, which describes the significance of the year 1601. Good background reading.
var accountExpires = 129508380000000000;
var dt = new DateTime(1601, 01, 01, 0, 0, 0, DateTimeKind.Utc).AddTicks(accountExpires);
Original Accepted Answer
It's the number of ticks since Jan-02-1601.
DateTime dt = new DateTime(1601, 01, 02).AddTicks(129508380000000000);
You can use the FromFileTime method on the DateTime class, but watch out, when this field is set to not expire, it comes back as the Int64.MaxValue and doesn't work with either of these methods.
Int64 accountExpires = 129508380000000000;
DateTime expireDate = DateTime.MaxValue;
if (!accountExpires.Equals(Int64.MaxValue))
expireDate = DateTime.FromFileTime(accountExpires);
Some info for anyone who came here looking to set the AccountExpires value.
To clear the expiry is nice and easy:
entry.Properties["accountExpires"].Value = 0;
However if you try to directly write back an int64 / long:
entry.Properties["accountExpires"].Value = dt.ToFileTime();
You can get a 'COMException was unhandled - Unspecified error'
Instead write back the value as a string data type:
entry.Properties["accountExpires"].Value = dt.ToFileTime().ToString();
Be aware of the time of day you are setting, for consistancy with ADUC the time should be 00:00.
Instead of .Now or .UtcNow you can use .Today:
var dt1 = DateTime.Today.AddDays(90);
entry.Properties["accountExpires"].Value = dt1.ToFileTime().ToString();
Other input like dateTimePicker you can replace the time, Kind as Local for the Domain Controller:
var dt1 = dateTimePicker1.Value;
var dt2 = new DateTime(dt1.Year, dt1.Month, dt1.Day, 0, 0, 0, DateTimeKind.Local);
entry.Properties["accountExpires"].Value = dt2.ToFileTime().ToString();
If you View Source on the link you posted you should see a Javascript conversion algorithm that should translate quite nicely to c#
For Ruby
def ldapTimeConverter(ldap_time)
Time.at((ldap_time/10000000)-11644473600)
end
I stumbled across this working on a PowerShell script. I found I can query the accountexpirationdate property and no conversion is required.
Someone had the "best" way above but when it's set to never expired, the value is zero.
public static DateTime GetAccountExpiresDate(DirectoryEntry de)
{
long expires = de.properties["accountExpires"].Value;
if (expires == 0) // doesn't expire
return DateTime.MaxValue;
return DateTime.FromFileTime(expires);
}
I'll provide a perfect answer to this question using which you will be able to convert and DateTime to active directory long int format and will be also able to do the vice versa of it.
Here is a solution to it:-
To get DateTime from AD
string tickstring = de.Properties["accountExpires"][0].ToString();
long Ticks = (long) tickstring;
DateTime ReferenceDate = new DateTime(1601, 1, 1, 0, 0, 0, DateTimeKind.Utc);
long ExpireDateTicks = Ticks + ReferenceDate.Ticks;
DateTime ExpireDate = new DateTime(ExpireDateTicks);
To convert DateTime to AD long integer format
DateTime ReferenceDate = new DateTime(1601, 1, 1, 0, 0, 0, DateTimeKind.Utc);
DateTime ExpireDate = new DateTime(Request.EndDate.Year, Request.EndDate.Month, Request.EndDate.Day, 0, 0, 0);
long Ticks = ExpireDate.Ticks - ReferenceDate.Ticks;
NewUser.accountExpires = Ticks.ToString();
I use this to convert DateTime value into Date and then I add 00:00:00 and 23:59:59 to make sure whole day is taken into consideration when counting stuff. I'm pretty sure it's wrong way of doing things. What would be the right way?
DateTime varObliczOd = DateTime.Parse(dateTimeWycenaPortfelaObliczDataOd.Value.ToShortDateString() + " 00:00:00");
DateTime varObliczDo = DateTime.Parse(dateTimeWycenaPortfelaObliczDataDo.Value.ToShortDateString() + " 23:59:59");
if dateTimeWycenaPortfelaObliczDataOd is of type DateTime, You can use:
dateTimeWycenaPortfelaObliczDataOd.Date
to get the date part only (time will be 00:00:00...).
If you want to get the very last tick of the date, you can use:
dateTimeWycenaPortfelaObliczDataOd.Date.AddDays(1).AddTicks(-1)
but you really better work with the next date (.AddDays(1)).
In any case, there is no need to convert to string and back to DateTime.
DateTime objects have a Date property which might be what you need.
You can use the following properties / methods on a DateTime object to get your values :
DateTime varObliczOd = dateTimeWycenaPortfelaObliczDataOd.Date;
DateTime varObliczDo = dateTimeWycenaPortfelaObliczDataOd.AddDayes(1).AddTicks(-1);
It would help to know why you're needing it, but this would work.
DateTime varObliczOd = dateTimeWycenaPortfelaObliczDataOd.Date;
DateTime varObliczDo = varObliczOd.AddDays(1).AddSeconds(-1);
Using the Date attribute and then manipulating them directly to create the required time component - no need to bother with parsing and conversion.
You could use the Date property of the DateTime object to accomplish what you need.
DateTime varObliczOd = dateTimeWycenaPortfelaObliczDataOd.Value.Date;
DateTime varObliczDo = dateTimeWycenaPortfelaObliczDataDo.Value.Date.AddDays(1);
If you really want it to end at 23:59:59 you can do:
DateTime varObliczDo = dateTimeWycenaPortfelaObliczDataDo.Value.Date.AddDays(1).AddSeconds(-1);
Will set varObliczDo to be your ending date with no time plus one day (at midnight). So if dateTimeWycenaPortfelaObliczDataDo was 2010-03-05 16:12:12 it would now be 2010-03-06 00:00:00.
Something like this maybe? I've typed this out of my head, there are probably some mistakes in the code.
DateTime varObliczOd = dateTimeWycenaPortfelaObliczDataOd.AddSeconds(-dateTimeWycenaPortfelaObliczDataOd.Seconds).AddMinutes(-dateTimeWycenaPortfelaObliczDataOd.Minutes).AddHours(-dateTimeWycenaPortfelaObliczDataOd.Hours);
DateTime varObliczDo = new DateTime(dateTimeWycenaPortfelaObliczDataDo.Year, dateTimeWycenaPortfelaObliczDataDo.Month, dateTimeWycenaPortfelaObliczDataDoDay, 23, 59, 59);
DateTime newDate = new DateTime( oldDate.Year, oldDate.Month, oldDate.Day, 23, 59,59 )
DateTime newDate = new DateTime( oldDate.Year, oldDate.Month, oldDate.Day, 0, 0, 0 )
You could work with TimeSpan:
DateTime varObliczOd = dateTimeWycenaPortfelaObliczDataOd - new TimeSpan(dateTimeWycenaPortfelaObliczDataOd.Hours, dateTimeWycenaPortfelaObliczDataOd.Minutes, dateTimeWycenaPortfelaObliczDataOd.Seconds);
Like that you avoid at least the parsing, which can fail depending on the local culture settings.