Showing posts with label subtracting. Show all posts
Showing posts with label subtracting. Show all posts

Wednesday, March 28, 2012

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 Date in VB.Net

Need Help!

How do I write the following expression for my project limiting the number of entries on my datagrid by 30 days from Now():

<!--

SELECT * FROM tblGrid WHERE dateEntered between Now() - 30 and Now()

-->

note: dateEntered are the dates in my date field (mm-dd-yyyy)

Thanks a lot.

If you are using VB.NET, you can use DateTime.Add method. There's an exampe in the docs here:

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

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 Quantities

Hi,

I have a query about updating quantity values within an website written in asp.net 1.1, vb2003 which references an access database. Basically what I want to do is when the customer confirms their order, I want then the number of items purchased to then be subtracted from the Products table in the database. Thus this ensures the number of each item in stock is kept up to date within the database.

My checkout table contains the intCartitemID (autonumber), Cart ID (random number and date value), quantity ordered and the productID for the item purchased.

The products table contains the ProductID, description, attributes, sale price and quantities in stock.

Now I assume I need to take the quantity of item purchased from the checkout table and then subtract this value from the products table. However, how do I put this idea into practice in terms of the asp.net coding ?

Any online examples of coding would help.

Thanks,

Just pass it in a SQL query

"Update productsTable set quantity = quantity - 1 where productID = " & productID


I agree with the above, except not "quantity = quantity - 1" ... but rather "quantity = quantity - @.howMuchWasOrdered"


Thanks both...Smile

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