Showing posts with label subtract. Show all posts
Showing posts with label subtract. Show all posts

Wednesday, March 28, 2012

Subtract days from a Date

How can I code my app to get this date:

11/1/2203

From this date:

11/15/2003

I need to Subtract 14 days from 11/15/2003 but I am not sure to go about doing this.

ThanksThis may help

Dim dteNewDate As Date
Dim dteOldDate As Date = "01/01/03"
Dim intNoOfDays As Int16 = 11

dteNewDate = DateAdd("d", -intNoOfDays, dteOldDate)

TextBox1.Text = dteNewDate
The easiest way to do this is to pass a negative number into Date.AddDays(). I was able to perform the transformation you described using the following code:


Private Sub Calendar1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calendar1.SelectionChanged
Label1.Text = Calendar1.SelectedDate.AddDays(-14)
End Sub

(This code should be run on a page with a Label and Calendar control if you want to try it out.)

Hope this helps.
<%
Dim dtmDate As DateTime
dtmDate = DateTime.Now()

Response.Write( dtmDate.ToString( "d" ) )

Dim AdddtmDate As DateTime

AdddtmDate = dtmDate.addDays(-14)

Response.write ("<br>")
Response.write (AdddtmDate)
%>
We recently wrote a simpletutorial example on working with Dates & Time in ASP.NET

Clay is right - just pass a negative number.

Bringing this back up. I found the information here very useful.

Can anyone tell me if there is a way to get what the last day of the month is? Like if the month is Febuary, I need to be able to detect 28 and 31 for March...


I don't think there is anything built in to get the last day of the month but you can use this little piece of code to get the last day of the month.

privateDateTime getLastDayOfMonth(DateTime d)

{

//get the first day of this month

int days = (d.Day - 1) * -1;

d = d.AddDays(days);

// add a month to this date

d = d.AddMonths(1);

d=d.AddDays(-1);

return d;

}

  

subtracting 2 times in string format

Hi,

I have 2 strings in "HH:mm" format, for example "03:55" or "22:09"

Is there any easy to way subtract 2 strings in this format ??

such that if string1 ="22:10"

and string2 = "01:30"

then answer should be string1 - string2

"20:40"

Thanks,

Split the strings into arrays, then do the math on the arrays.

string[] astrMySTring = DateString.Split(":");

then cast them as integers.

however, you might want to use the Date functions to do date Calculations... base 60 is a pain... crazy Babylonians.

Subtracting 2 Times

Hi AllI'm trying to subtract 2 times.

T1 = 3:50:00 PM

T2 = 4:00:00 PM

T3 = T2.Subtract(T1)


But I'm getting the errorCannot implicitly convert type 'System.TimeSpan' to 'System.DateTime'.

Please give me a solution


what type of variable is T3 defined as? T3 needs to be a TimeSpan not a date time. The subtraction of two times is a time span not a time itself. as in 1:24 - 1:12 = 12 minutes. 12 minutes is not a time it is a time span.

I hope that helps,
Brendan


Hi

By using Timespan I'm having the following codes. But i'm gettingerror message in t1, t2, t3

DateTime dt1, dt2, dt3;
dt1 = System.DateTime.Now;
dt2 = System.DateTime.Now;

TimeSpan t1, t2, t3;

t1 = System.Convert.ToDateTime(dt1).ToLongTimeString();
t2 = System.Convert.ToDateTime(dt2).ToLongTimeString();
t3 = t2.Subtract(t1);
Label3.Text = t3;

Please rectify this.


Hi

The following code must work

T1 = 3:50:00 PM

T2 = 4:00:00 PM

TimeSpan tsp = T2.Subtract(T1);

Label3.Text = tsp.TotalHours.ToString() + ":" + tsp.TotalMinutes.ToString() + ":" + tsp.TotalSeconds.ToString()


Hi as you'll notice from my first post. I mentioned that t3 was likely a datetime, and it needs to be a timespan. The other 2 should be datetimes, so t1 and t2 are DateTimes. Then you make t3 a timespan.

DateTime t1, t2;TimeSpan t3;t3 = t2.Subtract(t1);Label3.Text = t3.ToString();

I hope that helps,
Brendan

Subtracting from a Date

Hello,

In .NET code, I need to subtract a month from the current date; is there something in the framework that does this; there is some care that is needed to do this, especially with 28-day and 30-day months, so I was wondering if there was something built-in.

Thanks.

If I understand you question right you could do this:

DateTime dtTemp = DateTime.Now;

int Month = dtTemp.Month;

Cheers

Al


Dim dAs DateTime

d = DateTime.Now

d = d.AddMonths(-1)


If you write VB.NET try the link below for DateDiff function a clone of the T-SQL version. Hope this helps.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vafctdatediff.asp


Hello,

Try it:

Dim LastMonth As Date = DateAdd(DateInterval.Month, -1, #3/27/2006#)

You'll found this function under following namespace:
Microsoft.VisualBasic

An this will solve your problem related to 28-days/30-days. Functions takes care of this.

Regards

Kuldeep Deokule

subtracting days from date

hI,
i AM TRYING TO SUBTRACT DAYS FROM PARTICULAR DATE. i KEEP GETTING THE ERROR:
hERE IS MY CODE: enddatecriteria=tempDate.AddDays(30);
startdatecriteria=tempDate.AddDays(-30);
I AM USING C# AND THE ERROR IS:
Ticks must be between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks.
Parameter name: ticks
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information abou
t
the error and where it originated in the code.
Exception Details: System.ArgumentOutOfRangeException: Ticks must be between
DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks. Parameter name: ticks
Source Error:
cAN SOMEONE HELPtempDate = System.DateTime.Now;
enddatecriteria=tempDate.AddDays(30);
startdatecriteria=tempDate.AddDays(-30);
"Manny Chohan" wrote:

> hI,
> i AM TRYING TO SUBTRACT DAYS FROM PARTICULAR DATE. i KEEP GETTING THE ERRO
R:
> hERE IS MY CODE: enddatecriteria=tempDate.AddDays(30);
> startdatecriteria=tempDate.AddDays(-30);
> I AM USING C# AND THE ERROR IS:
> Ticks must be between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks.
> Parameter name: ticks
> Description: An unhandled exception occurred during the execution of the
> current web request. Please review the stack trace for more information ab
out
> the error and where it originated in the code.
> Exception Details: System.ArgumentOutOfRangeException: Ticks must be betwe
en
> DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks. Parameter name: ticks
> Source Error:
> cAN SOMEONE HELP
my complete code is follows:
tempDate=Convert.ToDateTime(Calendar2.TodaysDate.ToShortDateString());
enddatecriteria=tempDate.AddDays(30);
startdatecriteria=tempDate.AddDays(-30);
It still give me error. I am using c#.
Thanks
Manny
"vinay" wrote:
> tempDate = System.DateTime.Now;
> enddatecriteria=tempDate.AddDays(30);
> startdatecriteria=tempDate.AddDays(-30);
>
> "Manny Chohan" wrote:
>
Which event are you checking the values'
post the full code.
"Manny Chohan" wrote:
> my complete code is follows:
> tempDate=Convert.ToDateTime(Calendar2.TodaysDate.ToShortDateString());
> enddatecriteria=tempDate.AddDays(30);
> startdatecriteria=tempDate.AddDays(-30);
> It still give me error. I am using c#.
> Thanks
> Manny
> "vinay" wrote:
>
thanks Vinay, here is the code:
if(!Page.IsPostBack)
{
Get_Appointments();
tempDate=Convert.ToDateTime(Calendar2.TodaysDate.ToShortDateString());
}
public void MonthChanged(object sender, MonthChangedEventArgs e)
{
//Set the tempDatevariable to the value in the MonthChangedEventArgs
NewDate property
tempDate = e.NewDate;
//Reload the collection
Get_Appointments();
}
ICollection Get_Appointments()
{
DateTime startDate = new DateTime(2004,11,1);
DateTime endDate = new DateTime(2004,12,1);
enddatecriteria=tempDate.AddDays(30);
startdatecriteria=tempDate.AddDays(-30);
Rest follows the custom code as i am working with vendor API. Please let me
know if this is enough.
Thanks
Manny
"vinay" wrote:
> Which event are you checking the values'
> post the full code.
> "Manny Chohan" wrote:
>
Manny, looks like your tempdate is empty when you do the startdatecriteria=
(you're trying to back off 30 days from what's already the date.minvalue).
I'd trace backwards from there and look at calendar2.todaysdate and
e.newdate. hth
"Manny Chohan" wrote:
> thanks Vinay, here is the code:
> if(!Page.IsPostBack)
> {
> Get_Appointments();
> tempDate=Convert.ToDateTime(Calendar2.TodaysDate.ToShortDateString()
);
> }
> public void MonthChanged(object sender, MonthChangedEventArgs e)
> {
> //Set the tempDatevariable to the value in the MonthChangedEventArgs
> NewDate property
> tempDate = e.NewDate;
> //Reload the collection
> Get_Appointments();
> }
> ICollection Get_Appointments()
> {
> DateTime startDate = new DateTime(2004,11,1);
> DateTime endDate = new DateTime(2004,12,1);
> enddatecriteria=tempDate.AddDays(30);
> startdatecriteria=tempDate.AddDays(-30);
> Rest follows the custom code as i am working with vendor API. Please let m
e
> know if this is enough.
> Thanks
> Manny
>
> "vinay" wrote:
>

subtracting days from date

hI,
i AM TRYING TO SUBTRACT DAYS FROM PARTICULAR DATE. i KEEP GETTING THE ERROR:
hERE IS MY CODE: enddatecriteria=tempDate.AddDays(30);
startdatecriteria=tempDate.AddDays(-30);
I AM USING C# AND THE ERROR IS:
Ticks must be between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks.
Parameter name: ticks
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Ticks must be between
DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks. Parameter name: ticks

Source Error:

cAN SOMEONE HELPtempDate = System.DateTime.Now;
enddatecriteria=tempDate.AddDays(30);
startdatecriteria=tempDate.AddDays(-30);

"Manny Chohan" wrote:

> hI,
> i AM TRYING TO SUBTRACT DAYS FROM PARTICULAR DATE. i KEEP GETTING THE ERROR:
> hERE IS MY CODE: enddatecriteria=tempDate.AddDays(30);
> startdatecriteria=tempDate.AddDays(-30);
> I AM USING C# AND THE ERROR IS:
> Ticks must be between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks.
> Parameter name: ticks
> Description: An unhandled exception occurred during the execution of the
> current web request. Please review the stack trace for more information about
> the error and where it originated in the code.
> Exception Details: System.ArgumentOutOfRangeException: Ticks must be between
> DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks. Parameter name: ticks
> Source Error:
> cAN SOMEONE HELP
my complete code is follows:
tempDate=Convert.ToDateTime(Calendar2.TodaysDate.T oShortDateString());

enddatecriteria=tempDate.AddDays(30);
startdatecriteria=tempDate.AddDays(-30);
It still give me error. I am using c#.
Thanks
Manny

"vinay" wrote:

> tempDate = System.DateTime.Now;
> enddatecriteria=tempDate.AddDays(30);
> startdatecriteria=tempDate.AddDays(-30);
>
> "Manny Chohan" wrote:
> > hI,
> > i AM TRYING TO SUBTRACT DAYS FROM PARTICULAR DATE. i KEEP GETTING THE ERROR:
> > hERE IS MY CODE: enddatecriteria=tempDate.AddDays(30);
> > startdatecriteria=tempDate.AddDays(-30);
> > I AM USING C# AND THE ERROR IS:
> > Ticks must be between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks.
> > Parameter name: ticks
> > Description: An unhandled exception occurred during the execution of the
> > current web request. Please review the stack trace for more information about
> > the error and where it originated in the code.
> > Exception Details: System.ArgumentOutOfRangeException: Ticks must be between
> > DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks. Parameter name: ticks
> > Source Error:
> > cAN SOMEONE HELP
Which event are you checking the values??
post the full code.

"Manny Chohan" wrote:

> my complete code is follows:
> tempDate=Convert.ToDateTime(Calendar2.TodaysDate.T oShortDateString());
> enddatecriteria=tempDate.AddDays(30);
> startdatecriteria=tempDate.AddDays(-30);
> It still give me error. I am using c#.
> Thanks
> Manny
> "vinay" wrote:
> > tempDate = System.DateTime.Now;
> > enddatecriteria=tempDate.AddDays(30);
> > startdatecriteria=tempDate.AddDays(-30);
> > "Manny Chohan" wrote:
> > > hI,
> > > i AM TRYING TO SUBTRACT DAYS FROM PARTICULAR DATE. i KEEP GETTING THE ERROR:
> > > hERE IS MY CODE: enddatecriteria=tempDate.AddDays(30);
> > > startdatecriteria=tempDate.AddDays(-30);
> > > I AM USING C# AND THE ERROR IS:
> > > Ticks must be between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks.
> > > Parameter name: ticks
> > > Description: An unhandled exception occurred during the execution of the
> > > current web request. Please review the stack trace for more information about
> > > the error and where it originated in the code.
> > > > Exception Details: System.ArgumentOutOfRangeException: Ticks must be between
> > > DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks. Parameter name: ticks
> > > > Source Error:
> > > > cAN SOMEONE HELP
thanks Vinay, here is the code:

if(!Page.IsPostBack)
{
Get_Appointments();
tempDate=Convert.ToDateTime(Calendar2.TodaysDate.T oShortDateString());

}
public void MonthChanged(object sender, MonthChangedEventArgs e)
{
//Set the tempDatevariable to the value in the MonthChangedEventArgs
NewDate property
tempDate = e.NewDate;
//Reload the collection
Get_Appointments();
}

ICollection Get_Appointments()
{

DateTime startDate = new DateTime(2004,11,1);
DateTime endDate = new DateTime(2004,12,1);

enddatecriteria=tempDate.AddDays(30);
startdatecriteria=tempDate.AddDays(-30);

Rest follows the custom code as i am working with vendor API. Please let me
know if this is enough.

Thanks

Manny

"vinay" wrote:

> Which event are you checking the values??
> post the full code.
> "Manny Chohan" wrote:
> > my complete code is follows:
> > tempDate=Convert.ToDateTime(Calendar2.TodaysDate.T oShortDateString());
> > enddatecriteria=tempDate.AddDays(30);
> > startdatecriteria=tempDate.AddDays(-30);
> > It still give me error. I am using c#.
> > Thanks
> > Manny
> > "vinay" wrote:
> > > tempDate = System.DateTime.Now;
> > > enddatecriteria=tempDate.AddDays(30);
> > > startdatecriteria=tempDate.AddDays(-30);
> > > > > "Manny Chohan" wrote:
> > > > > hI,
> > > > i AM TRYING TO SUBTRACT DAYS FROM PARTICULAR DATE. i KEEP GETTING THE ERROR:
> > > > hERE IS MY CODE: enddatecriteria=tempDate.AddDays(30);
> > > > startdatecriteria=tempDate.AddDays(-30);
> > > > I AM USING C# AND THE ERROR IS:
> > > > Ticks must be between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks.
> > > > Parameter name: ticks
> > > > Description: An unhandled exception occurred during the execution of the
> > > > current web request. Please review the stack trace for more information about
> > > > the error and where it originated in the code.
> > > > > > Exception Details: System.ArgumentOutOfRangeException: Ticks must be between
> > > > DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks. Parameter name: ticks
> > > > > > Source Error:
> > > > > > cAN SOMEONE HELP
Manny, looks like your tempdate is empty when you do the startdatecriteria=
(you're trying to back off 30 days from what's already the date.minvalue).
I'd trace backwards from there and look at calendar2.todaysdate and
e.newdate. hth

"Manny Chohan" wrote:

> thanks Vinay, here is the code:
> if(!Page.IsPostBack)
> {
> Get_Appointments();
> tempDate=Convert.ToDateTime(Calendar2.TodaysDate.T oShortDateString());
> }
> public void MonthChanged(object sender, MonthChangedEventArgs e)
> {
> //Set the tempDatevariable to the value in the MonthChangedEventArgs
> NewDate property
> tempDate = e.NewDate;
> //Reload the collection
> Get_Appointments();
> }
> ICollection Get_Appointments()
> {
> DateTime startDate = new DateTime(2004,11,1);
> DateTime endDate = new DateTime(2004,12,1);
> enddatecriteria=tempDate.AddDays(30);
> startdatecriteria=tempDate.AddDays(-30);
> Rest follows the custom code as i am working with vendor API. Please let me
> know if this is enough.
> Thanks
> Manny
>
> "vinay" wrote:
> > Which event are you checking the values??
> > post the full code.
> > "Manny Chohan" wrote:
> > > my complete code is follows:
> > > tempDate=Convert.ToDateTime(Calendar2.TodaysDate.T oShortDateString());
> > > > enddatecriteria=tempDate.AddDays(30);
> > > startdatecriteria=tempDate.AddDays(-30);
> > > It still give me error. I am using c#.
> > > Thanks
> > > Manny
> > > > "vinay" wrote:
> > > > > tempDate = System.DateTime.Now;
> > > > enddatecriteria=tempDate.AddDays(30);
> > > > startdatecriteria=tempDate.AddDays(-30);
> > > > > > > > "Manny Chohan" wrote:
> > > > > > > hI,
> > > > > i AM TRYING TO SUBTRACT DAYS FROM PARTICULAR DATE. i KEEP GETTING THE ERROR:
> > > > > hERE IS MY CODE: enddatecriteria=tempDate.AddDays(30);
> > > > > startdatecriteria=tempDate.AddDays(-30);
> > > > > I AM USING C# AND THE ERROR IS:
> > > > > Ticks must be between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks.
> > > > > Parameter name: ticks
> > > > > Description: An unhandled exception occurred during the execution of the
> > > > > current web request. Please review the stack trace for more information about
> > > > > the error and where it originated in the code.
> > > > > > > > Exception Details: System.ArgumentOutOfRangeException: Ticks must be between
> > > > > DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks. Parameter name: ticks
> > > > > > > > Source Error:
> > > > > > > > cAN SOMEONE HELP

subtracting queries

I have 2 sql queries, and want to subtract one from the other, if that makes sense, i'm not too sure of the correct terminology.

The results of both queries have a common field, PlayerId.

query1 (select PlayerId, FirstName, LastName from Table1 where ....)

query2 (select PlayerId from Table2 where ....)

Thanks

RamilaYou could do a negation on an IN or Exists query for example:

Select PlayerID, FirstName, etc... FROM Table1 WHERE PlayerID Not IN(Select PlayerID from Table2...)

subtracting two dates to get number of days between

How would i take two dates startdate and enddate and subtract startdate from
enddate to figure the number of days between the two? thanks["Followup-To:" header set to microsoft.public.dotnet.languages.vb.]
On 2003-11-19, Brian Henry <brianiup@.adelphia.net> wrote:
> How would i take two dates startdate and enddate and subtract startdate from
> enddate to figure the number of days between the two? thanks

Dim Difference As TimeSpan = enddate.Subtract(startdate)
Console.WriteLine(Difference.Days)

--
Tom Shelton
[MVP - Visual Basic]
neverminde :)

If DateDiff(DateInterval.Day, dbReader("sectionedited"), Now.Date) <= 30
Then

"Brian Henry" <brianiup@.adelphia.net> wrote in message
news:uEyFv6trDHA.2360@.TK2MSFTNGP10.phx.gbl...
> How would i take two dates startdate and enddate and subtract startdate
from
> enddate to figure the number of days between the two? thanks
Not sure what language you want, but the VB isn't much differen that this C#

DateTime d1, d2; // Make sure you assign these first

TimeSpan ts = d1-d2;

int days = ts.Days;

HTH
Brian W

"Brian Henry" <brianiup@.adelphia.net> wrote in message
news:uEyFv6trDHA.2360@.TK2MSFTNGP10.phx.gbl...
> How would i take two dates startdate and enddate and subtract startdate
from
> enddate to figure the number of days between the two? thanks
((start.Ticks - end.Ticks) / TimeSpan.TicksPerHour) /24;

"Brian Henry" <brianiup@.adelphia.net> wrote in message
news:uEyFv6trDHA.2360@.TK2MSFTNGP10.phx.gbl...
> How would i take two dates startdate and enddate and subtract startdate
from
> enddate to figure the number of days between the two? thanks
System.DateTime includes a Subtract method, which will do exactly this. In
C#, you can also use the - operator.

--
Chris Jackson
Software Engineer
Microsoft MVP - Windows Shell/UI
Windows XP Associate Expert
--
More people read the newsgroups than read my email.
Reply to the newsgroup for a faster response.
(Control-G using Outlook Express)
--

"Brian Henry" <brianiup@.adelphia.net> wrote in message
news:uEyFv6trDHA.2360@.TK2MSFTNGP10.phx.gbl...
> How would i take two dates startdate and enddate and subtract startdate
from
> enddate to figure the number of days between the two? thanks
when i do this

Dim diffNUmb As Integer = DateDiff(DateInterval.day,
dbReader.GetDateTime(dbReader.GetOrdinal("sectionedited")), Now())

the sectionedited attribute of the table in the dbreader being a datetime
with a date/time of #10/19/2003 12:00:00 AM# in it and i run it, every time
it will return 0 as the diffrence in days even though i change the month and
day in the fixed date... why would it do this? thanks

"Brian Henry" <brianiup@.adelphia.net> wrote in message
news:%23Ko5QBurDHA.540@.tk2msftngp13.phx.gbl...
> neverminde :)
> If DateDiff(DateInterval.Day, dbReader("sectionedited"), Now.Date) <= 30
> Then
>
> "Brian Henry" <brianiup@.adelphia.net> wrote in message
> news:uEyFv6trDHA.2360@.TK2MSFTNGP10.phx.gbl...
> > How would i take two dates startdate and enddate and subtract startdate
> from
> > enddate to figure the number of days between the two? thanks