Convert Time to a string format VB.net - vb.net

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.

Related

String value wrongly converted to datetime using vb.net

So I have this datetime value of 9.3.2016 18:56:12, by using datetime.parse, I can get the values but instead of getting '3' as the month, it takes '9' as month and '3' as day which is incorrect.
dim d1 as string = "9.3.2016 18:56:12"
dim d2 as datetime = datetime.parse(d1, CultureInfo.InvariantCulture)
I don't want to use datetime.parseExact because I'm having more than 1 value in the database.
Please help :( thank you!
Since you current culture appears to interpret dates in the way you expect you could simply do this,
Dim dateString = "9.3.2016 18:56:12"
Dim dateValue = DateTime.Parse(dateString)
By not specifying the InvariantCulture, you instruct DateTime.Parse to use the current culture which, in your case, interprets the date string correctly.
Sorry I've got the solution.
dim d2 as datetime = convert.todatetime(d1)

Remove 1 day from date

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")

Removing characters from date in visual basic

Im a complete NOOB in VB so please excuse the newbie question
Im running the following code which produces the current system date, as you can see in image below.
Dim cyear As Date
cyear = Date.Now
MsgBox(cyear)
My Question
I'm looking for a way to remove all the characters in the textbox above so that only the highlighted yellow numbers will remain. Which represents the last 2 digits of the current year.
You'll have to use a date format string, e.g.:
Dim value = String.Format("{0:yy}", DateTime.Now)
or
Dim value = DateTime.Now.ToString("yy")
Have a look at Custom Date and Time Format Strings.
Format the date before you output it:
Dim cyear As Date
cyear = Date.Now
Dim yearShort as string = cyear.ToString("yy")
MsgBox(yearShort)
For more formats, read here: http://msdn.microsoft.com/library/8kb3ddd4%28v=vs.110%29.aspx
It's best to simply pass a format to the tostring method.
DateTime.Now.toString("yy")
If that doesn't work...
DateTime.Now.toString("yyyy").Substring(2,2)
http://msdn.microsoft.com/en-us/library/zdtaw1bw(v=vs.110).aspx

Convert string date to show 2 year digits

i have a string which has a value of "08-06-2008". I want the result to look like "08-06-08". Is there a way to do this? I've tried CDate but that only gives me 8/06/2008 which doesnt resolve the issue.
Parse it to Date and back to string:
Dim dt As Date = Date.ParseExact("08-06-2008", "MM-dd-yyyy", CultureInfo.InvariantCulture)
Dim result As String = dt.ToString("MM-dd-yy", CultureInfo.InvariantCulture)
Since that is a normal format you could also omit the format string and use Date.Parse directly:
Dim dt As Date = Date.Parse("08-06-2008", CultureInfo.InvariantCulture)
I have used CultureInfo.InvariantCulture to avoid localization issues, normally your current culture is used in Parse/ParseExact and ToString.
See: Custom Date and Time Format Strings
Firstly, avoid CDate. It is a hangover from VB6, and is almost certainly less efficient than using the .net equivalent.
The following should give you the right answer:
string value = DateTime.ParseExact("08-06-2008", "dd-MM-yyyy").ToString("dd-MM-yy")
Note that your existing date format is ambiguous, so I've been unable to tell if you're meaning the 6th of August or the 8th of June - this is why I've used ParseExact over Parse. ParseExact is also slightly more efficient, as it tells the framework which format to use, rather than it having to guess.
Try this
Dim FormatString As String = ""
Dim SampleDate As DateTime
SampleDate = Now()
FormatString = Format(SampleDate,"dd-MM-yy")

VB.NET Get Only Year from Date

I'm pulling into a variable a datetime value. Now, I want to post this value back to my database, but I need it to be only the year digits. How do I get VB.NET to trim the month, day, and time off and just return the four character year (e.g. 2011)?
Date.Today.Year is what you're looking for, or for an existing date, just someDateVariable.Year
i know this is a bit late to answer
but, it wont hurt telling, try "year(now) " and load it to a variable
Example:
Dim Year_in_Digits = Year(Now)
Dim myDate As DateTime = #1/1/2011#
Dim myYear As Int32 = myDate.Year
There is a property on the DateTime structure called 'Year'. This returns an integer representing just the year.
If you need to convert this to a string, just use the ToString() function.
So..
MyDT.Year.ToString()
That's a c# example, I'm sure VB.Net is going to be very similar.
I just had to do this for my VB program.
Dim Year As Integer
Year = Convert.ToInt32(Now.ToString("yyyy"))
Use just "yy" if you want two digit year. Then, when you need to display it somewhere:
year.tostring
I hope this will help because I tested it
Dim nowYear As Integer = Date.Now.Year
I know it's late,but am sure i will help someone
To get exact year in vb:
Dim date As String=Now.Year.ToString()