Sharepoint 2010 dates and calculated fields - sharepoint-2010

For reasons I don't pretend to understand calculated fields that are set to return a date return the value in code in this format:
"datetime;#2015-04-25 00:00:00"
so dim myDate as datetime = oMasterItem("Contract End Date") fails, and you can't cast the value either.
How do I convert that to a real date format without doing string manipulation ?
(or am I missing something obvious?)
Many thanks!

Check this out:
SharePoint - get value of calculated field without manual parsing
You can do it by:
SPFieldCalculated cf = (SPFieldCalculated)myItem.Fields["CIDandTitle"];
string value = cf.GetFieldValueForEdit(myItem["CIDandTitle"]);
or
string value = cf.GetFieldValueAsText(myItem["CIDandTitle"]);

Related

How to compare two datetime value instead of string using VB.Net

How to compare two datetime values?
For example as per below condition, I have FDatetimes value is string coming (i.e 2021-06-21 17:00:30Z) from database and SDatetimes value should be Datetime.UtcNow. So my requirement is both left and right side always compare with datetime instead of string.
Also I have tried to use Convert.ToDateTime(FDatetimes). But there is one issue here. Whenever I have used the above function, That time the time is getting changed to 2021-06-21 10:00:30Z. My main aim is to compare both field value as DateTime instead of String. Please help in VB.Net
If FDatetimes < SDatetimes Then
'My logic
End If
So to resolve DST issues I did this (which I ran at 9:20AM local, 14:20 UTC on 23 Jun 2021)
Dim SDatetimes As DateTime = DateTime.UtcNow
Dim FDatetimes As String = "2021-06-23 15:20:30Z"
Dim FDT As DateTime = DateTime.ParseExact(FDatetimes,
"yyyy-MM-dd HH:mm:ssZ",
Globalization.CultureInfo.InvariantCulture,
Globalization.DateTimeStyles.AdjustToUniversal Or
Globalization.DateTimeStyles.AssumeUniversal)
Debug.WriteLine(SDatetimes.ToString)
Debug.WriteLine(FDT.ToString)
If FDT < SDatetimes Then
'My logic
Stop
End If

Compare code date to sql date ASP NET

I get the error about convert type date when i try to compare this :
Page Load :
Dim DataCorrenteLoad As String = DateTime.Now.ToString("dd-MM-yyyy")
Label2.Text = DataCorrenteLoad
and in SqlDataSource the where condition is that the FinishDate >= Today date
How can i solve this situation ? Should i change the format dd-mm-yyyy ? Or what ?
Thanks.
Seems, you want to compare dates, but you kept your focus on string representation of a date. This is not the same!
Take a look at example:
Dim DatabaseDate As DateTime = DateTime.Today.AddDays(-5)
Dim CurrentDate As DateTime = DateTime.Today
'case 1: compare and display date in current culture - depending on regional settings
Console.WriteLine("{0} {1} equal to {2}", DatabaseDate.ToString("d"), If(DatabaseDate=CurrentDate, "is", "is not") , CurrentDate.ToString("d"))
'case 2: change string representation of date:
'compare dates in France and German format
Dim cu1 As Globalization.CultureInfo = New Globalization.CultureInfo("Fr-fr")
Dim cu2 As Globalization.CultureInfo = New Globalization.CultureInfo("De-de")
DatabaseDate = DateTime.Today
Console.WriteLine("{0} {1} equal to {2}", DatabaseDate.ToString("d", cu1), If(DatabaseDate=CurrentDate, "is", "is not") , CurrentDate.ToString("d", cu2))
Above code returns:
'case 1
2017-06-08 is not equal to 2017-06-13
'case 2
13/06/2017 is equal to 13.06.2017
Conclusion:
Depending on OS regional settings, the date format may differ, but the date is still the same!
For further details, i'd strongly recommend to read these:
Standard Date and Time Format Strings
Custom Date and Time Format Strings
Parsing Date and Time Strings in .NET
Design and Implementation Guidelines for Web Clients
Formatting Date and Time for a Specific Culture
Label2.Text= System.DateTime.Now.ToString("dd-mm-yyyy");
use this it will work System.DateTime.Now will show current time when you use To strong("dd-mm-yyyy") it will convert it in to string and set it's formate which u set under double quotes.

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)

How to change date format vb.net 2015

I want to filter SQL-table between start date and end date, I used before string variable then I use string.format to make the format mm/dd/yyyy, I tried now in VB.net 2015 the following code:
Dim S as String
s=inputbox("Enter Start date")
S=string.format(S,"mm/dd/yyyy")
But it doesn't work, can somebody give me a solution?
You could try this for handling the input value, assuming you only need the date value as a formatted string, since your question is about formatting a date:
Dim S As String
S = InputBox("Enter Start date")
If IsDate(S) = True Then
Dim d As Date = Date.Parse(S)
S = d.ToString("mm/dd/yyyy")
Else
'Handle the non date input here
End If
But I think you should consider #Plutonix comment, since we don't know exactly how you are sending the date to perform the filtering, or how your table fields are defined.
Regards!

Convert SQL date datatype to string

I am writing something in VB and need to convert a SQL date (not datetime) into a string. This should be an easy convertion, however toString does not work and I cannot find anything online .
The part of the code I'm working on looks like this:
Dim incomingDate As String
incomingDate = row.Cells(5).Text.ToString()
When the data being put in the gridview is a DateTime datatype this works fine. If it's simply Date, it gives me the following error message:
"Specified argument was out of the range of valid values.
Parameter name: index"
I've also tried this work-around but it didn't work
Dim incomingDate As String
Dim d As New DateTime
d = DateTime.Parse(row.Cells(5).Text)
incomingDate = Date.Parse(row.Cells(5).Text)
Same error...
In SQL you can use convert(varchar, mySqlDate, 101) to emit a string instead of a date. Is that what you had in mind?