I just want to add 1 day to a DateTime. So I wrote:
DateTime date = new DateTime(2010, 4, 29, 10, 25, 00);
TimeSpan t = new TimeSpan(1, 0, 0, 0);
date.Add(t);
Console.WriteLine("A day after the day: " + date.ToString());
I thought the result would be: 2010 04 30- 10:25:00 but I'm still getting the initial date.
What's wrong?
DateTime values are immutable. The Add method returns a new DateTime value with the TimeSpan added.
This works:
Console.WriteLine("A day after the day: " + date.Add(t).ToString());
You need to change a line:
date = date.Add(t);
dtb is right about DateTime being immutable. Think of it this way: a DateTime is a value type, which puts it in the same category as int or double. Instances of these structures cannot be modified; they can only be evaluated and copied.
Consider this code:
int i = 4;
i + 2; // does not compile, but what if it did?
// would i become 6? clearly not --
// i + 2 expresses a NEW value, which can
// be copied somewhere
i = i + 2; // there we go -- that's better
This is analogous to:
DateTime d = DateTime.Now;
TimeSpan t = TimeSpan.FromDays(1.0);
d.Add(t); // compiles (because AddDays is a function),
// but is really the same as i + 2 above
d = d.Add(t); // that's better
By the way, one thing that might help make this clearer is realizing that the above line, d = d.Add(t), is the same as d = d + t. And you wouldn't write d + t on its own line, just like you wouldn't write i + 2 on its own line.
A DateTime is immutable, but the Add and Subtract functions return new DateTimes for you to use.
DateTime tomorrow = DateTime.Now.AddDays(1);
What is wrong with just doing date = date.AddDays(1)?
The result of date.Add(t) is what you're after:
DateTime date = new DateTime(2010, 4, 29, 10, 25, 00);
TimeSpan t = new TimeSpan(1, 0, 0, 0);
// The change is here, setting date to be the *new* date produced by calling Add
date = date.Add(t);
Console.WriteLine("A day after the day: " + date.ToString());
date.Add(t);
returns a modified DateTime and does not change the original instance on which you call the Add method on.
DateTime wont work if DateTime obj datatype is "DateTime?" which takes accepts null values, in such case DateTime? dt = DateTime.Now;
DateTime dateObj = new DateTime();
dateObj = Convert.ToDateTime(dt.ToString());
var Month3 = dateObj.AddMonths(3);`
Related
I am trying to simply subtract two dates. Probably this is a messy way of doing it. It says it cannot convert to double even though.
DateTime daysPlus14days = _dal.getOptinDate(new Guid(_myuser.id.ToString())).AddDays(14);
DateTime currentDate = DateTime.Now;
DateTime timeLeft = (daysPlus14days - currentDate).TotalDays
This just basically goes to db and gets me the date they created there account. Its just to work out how many days left they have 14 days to click a button other wise it will vanish.
public DateTime getOptinDate(Guid id)
{
var q = _dal.portalEntities.tblPortalUsers.Where(a => a.id == id).FirstOrDefault();
return (DateTime)q.optinDateStart;
}
just change this line:
double timeLeft = (daysPlus14days - currentDate).TotalDays;
TotalDays returns double and not DateTime
Refer this link https://msdn.microsoft.com/en-us/library/system.timespan.totaldays(v=vs.110).aspx,
System.DateTime date1 = new System.DateTime(1996, 6, 3, 22, 15, 0);
System.DateTime date2 = new System.DateTime(1996, 12, 6, 13, 2, 0);
System.TimeSpan diff1 = date2.Subtract(date1);
double totaldays = diff1.TotalDays;
I am looking for a elegant solution (if it exists) to achieve what I described in the title.
I saw an elegant solution for "changing the time in a DateTime object" and it is as follows:
DateTime s = ...;
TimeSpan ts = new TimeSpan(10, 30, 0);
s = s.Date + ts;
If there such a solution for changing the date in a DateTime object?
DateTime struct is designed to be immutable, so you can't change it. You can get a new one based on values from the old one, which is exactly what your solution does.
You can make it a bit more clear by using DateTime constructor which takes all date and time values: year, month, day, hour, minute and seconds.
s = new DateTime(s.Year, s.Month, s.Day, 10, 30, 0);
s = new DateTime(2014, 10, 2, s.Hour, s.Minute, s.Second);
or you can use TimeOfDay property:
s = new DateTime(2014, 10, 2) + s.TimeOfDay;
Assuming that you are going to pass a new DateTime object to update your existing object, a function like this can work.
DateTime UpdateDate(DateTime existingDate, DateTime newDate)
{
return newDate.Date + existingDate.TimeOfDay;
}
myDatetime = new DateTime(year, month, day);
You can use the DateTime constructors, so you can preserve the original time.
See this example:
var firstDate = new DateTime(2015, 01, 01, 15, 0, 0);
var newDate = DateTime.Now;
// New date, original time.
firstDate = new DateTime(newDate.Year, newDate.Month, newDate.Day, firstDate.Hour, firstDate.Minute, firstDate.Second, firstDate.Millisecond);
Try this:
DateTime s = ...;
s = new DateTime(year, month, day) + s.TimeOfDay;
That preserves the time in the original DateTime.
Try this:
DateTime now = DateTime.Now;
DateTime dateOnly = now.Date;
TimeSpan time = now.TimeOfDay;
dateOnly = DateTime.Parse("5/15/15"); // or whatever date you choose
DateTime newDateTime = dateOnly + time;
Assuming I can not change service that returns data, I am left with
var date = "20140231";
var scope = DateTime.ParseExact(date, "yyyyMMdd", CultureInfo.CurrentCulture);
Clearly "20140231" is lazy way of saying end of February. What is the cleanest way to get last date of February with input of "20140231"?
There is 1 constraint - this should work with .net 2.0.
string date = "20140231";
DateTime result;
int year = Convert.ToInt32(date.Substring(0, 4));
int month = Convert.ToInt32(date.Substring(4, 2));
int day = Convert.ToInt32(date.Substring(6, 2));
result = new DateTime(year, month, Math.Min(DateTime.DaysInMonth(year, month), day));
February can have only 28 or 29 days depends on current year is a leap year or not.
It can't have 30 or 31 days in any year. That's why you can't parse your 20140231 string successfully.
You can clearly get the last day of February like;
DateTime lastDayOfFebruary = (new DateTime(2014, 2, 1)).AddMonths(1).AddDays(-1);
If your service always get year as a first 4 character, you can use .Substring() to get year and pass DateTime constructor as a year.
var date = "20140231";
string year = date.Substring(0, 4);
DateTime lastDayOfFebruary = (new DateTime(int.Parse(year), 2, 1)).AddMonths(1).AddDays(-1);
You could create a while, cut the date in pieces, and keep subtracting one from the day part until it is a valid date. This should really be fixed on the entry side though.
Try this:
var date = "20140231";
DateTime scope;
bool dateValid = DateTime.TryParseExact(date, "yyyyMMdd", CultureInfo.CurrentCulture, DateTimeStyles.None, out scope);
while (!dateValid)
{
string yearMonth = date.Substring(0, 4);
int day = Convert.ToInt32(date.Substring(6, 2));
if (day > 1)
{
day--;
}
else
{
break;
}
date = yearMonth + day.ToString().PadLeft(2, '0');
dateValid = DateTime.TryParseExact(date, "yyyyMMdd", CultureInfo.CurrentCulture, DateTimeStyles.None, out scope);
}
What is the best way in c# to get the same result of javascript date.gettime() call?
The getTime() method returns the number of milliseconds since midnight of January 1, 1970 and the specified date.
You can use this solution:
private int GetTime()
{
var time = (DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1));
return (int)(time.TotalMilliseconds + 0.5);
}
Since JavaScript time is with respect to UTC, I think you will need something like this:
var st = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
var t = (DateTime.Now.ToUniversalTime() - st);
// t.TotalMilliseconds
Now you can use the TotalMilliseconds property of the Timespan.
The correct implementation (assuming the current time) is as follows:
DateTime utcNow = DateTime.UtcNow;
DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
long ts = (long)((utcNow - epoch).TotalMilliseconds);
The Java and JavaScript Date.getTime() methods return the number of milliseconds since 1 Jan 1970 00:00:00 GMT.
Since .NET
represents dates in Ticks (1 Tick = 0.1 nanoseconds or 0.0001 milliseconds) since 1 Jan 0001 00:00:00 GMT, we must use a
conversion formula where 621355968000000000 is the offset between the base dates in Ticks and 10000 the number of Ticks per
Millisecond.
Ticks = (MilliSeconds * 10000) + 621355968000000000
MilliSeconds = (Ticks - 621355968000000000) / 10000
I guess this will do the trick :)
public double MilliTimeStamp(DateTime TheDate)
{
DateTime d1 = new DateTime(1970, 1, 1);
DateTime d2 = TheDate.ToUniversalTime();
TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks);
return ts.TotalMilliseconds;
}
(DateTime.Now - new DateTime (1970, 1, 1)).TotalMilliseconds
Now in C# you can use built-in function:
new DateTimeOffset(Your_DateTime_Variable_Here).ToUnixTimeMilliseconds()
So the sample code would be:
var dateToUse = DateTime.Now;
var javaGetTimeValue = new DateTimeOffset(dateToUse).ToUnixTimeMilliseconds()
The currently accepted answer returns an int which is incorrect. It has to be Int64 or long. This is just rewriting the correct answer provided by Matt Johnson-Pint (and edited by Adaptabi) as one line. Please accept Matt Johnson-Pint's answer. I checked it against actual javascript new Date().getTime() in the console to verify it returns the same number.
long JavascriptGetTime()
{
return (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
}
Here is an extension method based off Enigma State's answer
public static Int64 GetJavascriptTimeStamp(this DateTime dt)
{
var nineteenseventy = new DateTime(1970, 1, 1);
var timeElapsed = (dt.ToUniversalTime() - nineteenseventy);
return (Int64)(timeElapsed.TotalMilliseconds + 0.5);
}
To use it for the current time:
var timeStamp = DateTime.Now.GetJavascriptTimeStamp();
private static ulong GetTime()
{
const long INIT_DATA_TICKS = 621355968000000000; // 1.1.1970 in ticks
const double ROUNDINGS_FIX = 0.5;
TimeSpan dTicks = new TimeSpan(DateTime.UtcNow.Ticks - INIT_DATA_TICKS);
return (ulong)(dTicks.TotalMilliseconds + ROUNDINGS_FIX);
}
DateTime.Now.ToUniversalTime() use intern DateTime.UtcNow
I work in a bizarre and irrational industry where we need to be able to represent the time of day as 06:00:00 to 30:00:00 instead of 0:00:00 to 24:00:00. Is there any way to do this using the DateTime type? If I try to construct a date time with an hour value greater than 24 it throws an exception.
I think this should be a presentation issue only.
Allow your users to input data in this weird format, and immediately convert it to UTC. Do all calculations on the UTC times. Then create a ToString method to convert the results back into your weird format. You will probably also need some other utility methods and properties such as an implementation of WeirdDateTime.Day.
You could write a wrapper class around a DateTime and have all the conversion and utility methods you need on that class. I've had a go at starting it - by implementing parsing from a string in weird format. This isn't production code ready by any means, but perhaps it can give you a few ideas of how you could approach this:
class WeirdDateTime
{
public DateTime DateTime { get; set; }
public WeirdDateTime(int year, int month, int day, int hour, int minute, int second, DateTimeKind kind)
{
if (hour < 6 || hour >= 30)
throw new ArgumentException("Not a valid WeirdDateTime", "hour");
bool addDay;
if (hour >= 24)
{
addDay = true;
hour -= 24;
}
else
{
addDay = false;
}
DateTime dateTime = new DateTime(year, month, day, hour, minute, second, kind);
if (addDay)
dateTime = dateTime.AddDays(1);
DateTime = dateTime;
}
public static WeirdDateTime Parse(string s)
{
Regex regex = new Regex(#"(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})");
Match match = regex.Match(s);
if (!match.Success)
throw new FormatException("Not a valid WeirdDateTime");
int[] parts = match.Groups.Cast<Group>()
.Skip(1)
.Select(x => int.Parse(x.Value))
.ToArray();
int year = parts[0];
int month = parts[1];
int day = parts[2];
int hour = parts[3];
int minute = parts[4];
int second = parts[5];
return new WeirdDateTime(year, month, day, hour, minute, second, DateTimeKind.Unspecified);
}
public override string ToString()
{
throw new NotImplementedException("Write this!");
}
}
class Program
{
public static void Main()
{
WeirdDateTime weirdDateTime = WeirdDateTime.Parse("2010-01-19 27:00:00");
DateTime dateTimeUtc = weirdDateTime.DateTime.ToUniversalTime();
Console.WriteLine(dateTimeUtc);
}
}
How about use a TimeSpan instead ?
DateTime departure = new DateTime(2010, 6, 12, 18, 32, 0);
DateTime arrival = new DateTime(2010, 6, 13, 22, 47, 0);
TimeSpan travelTime = arrival - departure;
Console.WriteLine("{0} - {1} = {2}", arrival, departure, travelTime);
Then use the TotalHours property of the TimeSpan obj
I doubt you can do exactly what you're looking for, but I expect that you could make your own DateTime class that simply adds +6 hrs to the value. i.e. stores 00 - 24 internally, but the get/set methods make it seem like 06 - 30.
You should be using TimeSpan, not DateTime.
The format options for TimeSpan is
a: [days].[hours]:[minutes]:[seconds].[fractional seconds]
b: [days].[hours]:[minutes]:[seconds]
c: [days].[hours]:[minutes]
d: [days].[hours]
e: [days]
f: [hours]:[minutes]:[seconds].[fractional seconds]
g: [hours]:[minutes]:[seconds]
h: [hours]:[minutes]
Just have your business logic store/return DateTime.Hours.Add(6). You'll have to be aware of this in your display logic.
how 'bout using a normal DateTime to store the actual time, and writing a new class which stores (or derives from ) a DateTime, and has a ToString() which adjusts the output.
I for calculate Employ work hours use this function:
public string SumHours(string TimeIn, string TimeOut)
{
var parts = TimeIn.Split(':');
var hours = Int32.Parse(parts[0]);
var minutes = Int32.Parse(parts[1]);
var result = new TimeSpan(hours, minutes, 0);
TimeIn = result.ToString();
TimeSpan Hour1 = TimeSpan.Parse(TimeIn);
TimeSpan Hour2 = TimeSpan.Parse(TimeOut);
Hour1 = Hour1.Add(Hour2);
string HourtoStr = string.Format("{0:D2}:{1:D2}:{2:D2}", (Hour1.Days * 24 + Hour1.Hours), Hour1.Minutes, Hour1.Seconds);
return HourtoStr;
}