I'm trying to add a number of months to a date.
I've tried numerous ways of converting the date string and adding the months but I'm getting nowhere, very fast.
I have start_date which is a date string "YYYY-MM-DD"
And I have item.product.sub_period which is the number of months I'd like to add.
I've tried many ways but am unsure as the correct way to convert the string into a format that can be modified.
Any help is appreciated.
Try the following:
Date.strptime(start_date, "%Y-%m-%d") + item.product.sub_period.months
Just make sure item.product.sub_period returns an integer.
Neal, I believe you can do this:
require 'date'
x = 3
a = Date.new(2012,2,3)
b = a >> x
as long as your date is a Date.
Related
I have the date working fine, and I know how to remove a date when doing it one way. But, I need the date in a special format, so I am trying to figure out how to remove 1 day from it with the following code:
TDate.Text = DateTime.Now.ToString("MM/dd")
Ydate.Text = Datetime.Now.ToString("MM/dd")
How can I remove 1 day from the Ydate.text?
AddDay(-1)
so
DateTime.Now.AddDays(-1).ToString("MM/dd")
To remove one day use the AddDays function with a negative number.
Dim _today As Date = Date.Now
Dim _yesterday = _today.AddDays(-1)
Your example:
Ydate.Text = Datetime.Now.AddDays(-1).ToString("MM/dd")
New VB coder here, trying to check if todays date is more then 10 years past a date from a grabbed database entry, and display a message if it is.
The Database is already imported and set up in the VB application H ave made, and working, I made a report to display the information.
I am guessing I need to use the Datediff but I can't seem to get it to work, Thanks.
I will give my variable name here
Dim Custsince as Date
'From the Database here
CustSince = CustListodr("Custsince")
Thanks in advance, working with dates is not my strong point.
You can just use a TimeSpan directly by subtracting the dates:
Dim customerLength = DateTime.Now - Custsince
Dim approxYears = customerLength.TotalDays / 365
If the date read from the database is already stored in a Date variable, you can simply subtract one date from another to get the difference. The result of subtracting two dates is a TimeSpan object. TimeSpan objects contain useful properties that allow you to see how long the span of time is in various units (e.g. days, hours, minutes). For instance:
Dim date1 As Date = ...
Dim date2 As Date = Date.Now
Dim span As TimeSpan = date2 - date1
If span.TotalDays >= 3650 then ' Ten years
'...
End If
Alternatively, if you need to compare calendar years, rather than the actual span of time, you can compare the years from each date, like this:
If date2.Year - date1.Year >= 10 Then
'...
End If
If the date being read from the database is stored as a string, rather than as a Date value, you would need to use Date.Parse or Date.ParseExact to convert the string into a Date value.
I am trying to format a date to a specific string and the code is returning incorect outcome.
CDate(RDate).ToString("ddMMy")
is returning the year as 2 digits is there a way to make it return the correct just last digit ? I know that I could substring to get the result but I would like to use a format string to allow customer to create their own.
Just glue them together with String.Format():
Dim dt = CDate(RDate)
Dim s = String.Format("{0}{1}", dt.ToString("ddMM"), dt.Year Mod 10)
The 'y' just indicates that the leading 0 is to be removed if the year is between 0 and 9. There is no format for just getting the last digit of the year.
See the MSDN documentation for the available custom formats.
The Custom Format year formats are limited to returning a minimum of a 2 digit year except in the situation that Compentent_tech mentions where you are using y and the year is ending with a 00 to 09. you can check out the MSDN Documentation. You will need to use string manipulation to achieve what you want.
Using that fact you can try something like this.
Dim RDate As Date = Now.Date
Dim NDate As Date = New Date((RDate.Year Mod 10), RDate.Month, RDate.Day)
CDate(NDate).ToString("ddMMy")
try this code :
Dim dt As DateTime = DateTime.Now
CDate(dt).ToString("ddMMyy").Remove(4, 1)
this code removes first of 2 digit year value.
I have this query, that's been giving me some issues, it looks like this:
UPDATE servicecontracts
SET planned_duration = (to_char(due_date) - to_char(start_date) + 1)
,actual_duration =''
,progress = NULL
WHERE servicecontractsid = '263'
After some research, I managed to figure out what this query is trying to do, it' s just trying to find the planned duration, by subtracting the due date and the start date. Why, this is trying to do that by subtracting strings, I do not know. Also, the to_char function requires a second parameter.
So, anyway, now I need to find the planned_duration, but how do I do that. According to the Postgresql docs, the to_char function doesn't have an option to return an integer, if you set it to return text and then if you try to convert the string into an integer using explicit casts, like ::integer, you get an error because an integer can't have colons in there.
So, is there a way for to_char to return an integer that somehow represents the date, and then subtract the two?
If not, what should I do to carry this out?
I quote from the fine manual here
SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40.12-08');
Result: 982384720.12
But for computing intervals, there is simpler way:
SELECT (due_date - start_date)
Just subtracting two date types from each other should return their difference in days.
SET planned_duration = (due_date - start_date)
Not sure why to_char is being used, unless I'm missing something here.
I have a date in the future e.g. 13/10/2008 I need to subtract the current date (today is the 28/09/2010) minus 7 days, so thats 21/09/2010 minus 13/10/2008, which would equal erm, 720 something ?
But the current date won't always be 28/09/2010, obviously.
I need the code for this.
EDIT: When i said future I mean past :)
Sub Main()
Dim dt As DateTime = New DateTime(2008, 10, 13)
' be careful what you are subtracting from what
' the date you have is not in the future (year 2008)
' if the date is in the future: (dt.Subtract(DateTime.Now.AddDays(-7))).TotalDays
' or simply take the absolute value
Dim days As Double = (DateTime.Now.AddDays(-7).Subtract(dt)).TotalDays
Console.WriteLine(days)
End Sub
You will also notice that the TotalDays property is of type Double.
13/10/2008 is not exactly in the future :)
Sorry for using C# code, but:
(dateInFuture - DateTime.Now.AddDays(-7)).TotalDays
Should work. Of course the other way around if you mean in the past:
(DateTime.Now.AddDays(-7) - dateInPast).TotalDays
Dim ValidDate As Date =cDate("Tuesday, December 31, 2013") 'A date in Future
Dim date1 As New System.DateTime(ValidDate.Year, ValidDate.Month, ValidDate.Day)
Dim date2 = Now
Dim Diff1 As System.TimeSpan
Diff1 = date1.Subtract(date2)
Dim TotRemDays = (Int(Diff1.TotalDays))
MsgBox(TotRemDays)
"I need the code for this" seems a bit too much like "Plz give meh teh codez", and your "date in the future" seems a little bit in the past.
Anyway, you should investigate the relevant methods of the DateTime structure, in particular the Subtract method (both overloads, or in alternative its subtraction operator), and you should have a look at the TimeSpan structure too.
You could create a DateTime for the date of today, subtract a TimeSpan of 7 days to it, and then subtract such result to a DateTime representing your date in the future (or, if it is in the past, do the opposite). You'll get a TimeSpan representing the difference in time between the two dates, from which you can easily get the number of days using its Days property.
As other said, to do the first subtraction you can also use the AddDays method of the DateTime structure.