This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
c#: whats the easiest way to subtract time?
I want
MyNewDateValue = MyDateNow - MyDateInteger;
Example
Today is the 22nd of December 2012
If MyDateIneger value is 120, MyNewDateValue, will return the datetime 120 days ago.
MyNewDateValue = MyDateNow.AddDays(-MyDateInteger);
Please look into DateTime.AddDays method
DateTime oneTwentyDaysAgo = DateTime.Today.AddDays(-120);
or in general
DateTime nDaysAgo = DateTime.Today.AddDays(-N);
// where N is the number of days
MyNewDateValue = MyDateNow.AddDays(-120);
or
MyNewDateValue = MyDateNow.AddDays(myVar);
Try this frnd
DateTime dt = new DateTime();
dt = DateTime.Now;
DateTime newdt = new DateTime();
TimeSpan tim = new TimeSpan(120,0,0,0,0);
newdt = dt.Add(tim);
MessageBox.Show(newdt.ToString());
ADD.timespan will help you to add or subtract days from today.
Related
This question already has answers here:
Combine two datetime variables into one (up to seconds precision)
(4 answers)
Closed 9 years ago.
I have 2 values:
var dt1 = dtFromDate.Value;
var tm1 = tmFromTime.Value;
dt1 = 12/5/2013 12:00:00 AM
tm1 = 11/5/2013 9:00:00 AM
i want to make datetime as : 12/5/2013 9:00:00 AM
how can it be possible?
You can take the date part of dt1 by accessing Date, the time part of tm1 by accessing TimeOfDay and then combine them using +:
dt1.Date + tm1.TimeOfDay
You can create a new DateTime using overloaded constructor:
DateTime dt = new DateTime(
dt1.Year,
dt1.Month,
dt1.Day,
tm1.Hour,
tm2.Minute,
tm2.Second,
tm2.Millisecond);
You can use that code, using Date property to get just date part and Time property to get just time part:
var dt1 = DateTime.Parse("12/5/2013 12:00:00 AM"); // this is just a sample date
var tm1 = DateTime.Parse("11/5/2013 9:00:00 AM"); // this is just a sample date
var newDate = dt1.Date.Add(tm1.TimeOfDay); // the code to use
DateTime object is immutable so in order to get the date from one DateTime object, but the time from another - you must create new DateTime object.
There is two ways for doing it:
Create new DateTime with constructor overload:
DateTime date1 =
new DateTime(dt1.Year, dt1.Month, dt1.Day, tm1.Hour, tm1.Minute, tm1.Second);
Parse string to DateTime:
var dateToParse =
String.Concat(dt1.ToString("yyyy.MM.dd ", CultureInfo.InvariantCulture),
tm1.ToString("HH:mm:ss", CultureInfo.InvariantCulture));
var date1 =
DateTime.ParseExact(dateToParse, "yyyy.MM.dd HH:mm:ss", CultureInfo.InvariantCulture);
This question already has answers here:
strtotime equivalent in .NET
(6 answers)
Closed 9 years ago.
Have big problem I don´t know how to compare date in C# like PHP.
I want to convert this code in to C#:
$now = strtotime ("now");
$then = strtotime ("$date");
$difference = $now - $then ;
$num = ($difference/86400)/7;
$weeks = intval($num);
Which function should I use?
This link can´t find a matching function like strtotime.
Please help me to convert the code to C#.
Thanks in advance.
var now = DateTime.Now.TimeOfDay;
DateTime dt = DateTime.Parse(yourDateTime);
var then = dt.ToString("HH:mm");
// here you can do rest of the calculations
basically
dt.ToString("HH:mm"); // 24 hour clock
dt.ToString("hh:mm tt"); // 12 hour clock
Hope it will give you better understanding
I think you might be looking for something like below:
DateTime sDateTimeNow = DateTime.Now;//Gets the date time from the server now
DateTime sIn30Seconds = DateTime.Now.AddSeconds(30);//Gets date time and adds 30 Seconds
DateTime sIn30Hours = DateTime.Now.AddHours(30);//Gets date time and adds 30 hours
DateTime sIn30Days = DateTime.Now.AddDays(30);//Gets date time and adds 30 days
double dTotalDays = (sIn30Days - sDateTimeNow).TotalDays;
double dTotalHours = (sIn30Hours - sDateTimeNow).TotalHours;
double dTotalSeconds = (sIn30Seconds - sDateTimeNow).TotalSeconds;
This question already has answers here:
How can I convert a Unix timestamp to DateTime and vice versa?
(21 answers)
How do you convert epoch time in C#?
(14 answers)
Closed 9 years ago.
I have an integer 15791 which represents count of days since epoch and equals 27.03.2013, how can do this convert in C#?
public void method1()
{
...
int days_since_epoch = 15791;
// how convert `days_since_epoch` to "27.03.2013"
}
Thanks!
Adds a number of days to your epoch.
For example:
var epoch = new DateTime(...); // Your epoch (01/01/0001 or whatever)
var yourDate = epoch.AddDays(days_since_epoch);
Assuming your Epoch date is in a DateTime just use
DateTime epoch = new DateTime(1970,1,1);
int days_since_epoch = 15791;
DateTime converted = epoch.AddDays(days_since_epoch);
Simply use the AddDays method, and once you got your final date, format it as usual in the ToString().
Perhaps:
TimeSpan ts = TimeSpan.FromDays(15791);
DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).Add(ts);
DEMO
var date = new DateTime(1970,1,1).AddDays(15791);
Console.WriteLine(date.ToString("dd.MM.yyyy"));
Is it possible to add dates in C#?
(DateTime.Today.ToLongDateString() + 10)
I tried this but it doesn't work.
Do you want to add days?
DateTime newDate = DateTime.Today.AddDays(10);
Note that you get a new DateTime back!
MSDN
Use DateTime.Today.AddDays(10) or any of the other AddXXX functions on DateTime.
What is the unit of 10. If it is days; then
var todayPlus10Days = DateTime.Today.AddDays(10);
Use AddDays() method:
DateTime dt = DateTime.Today;
dt = dt.AddDays(10);
This question already has answers here:
How to change time in DateTime?
(29 answers)
Closed 9 years ago.
I have a DateTime variable:
DateTime date = DateTime.Now;
I want to change the time part of a DateTime variable. But when I tried to access time part (hh:mm:ss) these fields are readonly.
Can't I set these properties?
Use the constructor that allows you to specify the year, month, day, hours, minutes, and seconds:
var dateNow = DateTime.Now;
var date = new DateTime(dateNow.Year, dateNow.Month, dateNow.Day, 4, 5, 6);
you can't change the DateTime object, it's immutable. However, you can set it to a new value, for example:
var newDate = oldDate.Date + new TimeSpan(11, 30, 55);
date = new DateTime(date.year, date.month, date.day, HH, MM, SS);
I'm not sure exactly what you're trying to do but
you can set the date/time to exactly what you want in a number of ways...
You can specify 12/25/2010 4:58 PM by using
DateTime myDate = Convert.ToDateTime("2010-12-25 16:58:00");
OR if you have an existing datetime construct , say 12/25/2010 (and any random time) and you want to set it to 12/25/2010 4:58 PM, you could do so like this:
DateTime myDate = ExistingTime.Date.AddHours(16).AddMinutes(58);
The ExistingTime.Date will be 12/25 at midnight, and you just add hours and minutes to get it to the time you want.
It isn't possible as DateTime is immutable. The same discussion is available here: How to change time in datetime?