Removing time format in a date - c#-4.0

I am passing a date as a parameter where daydate is the parameter
string daydate
The content of daydate is 3/3/2017 12:00:00 AM
I am doing this to remove the time and still the time does not go away
string strCleanDate = String.Format("{0:M/d/yyyy}", daydate);
How can I remove the time?

That is because the formatter is ignored as the instance you are passing to string.Format is a string so the placeholder is filled with the value you passed in and the format is ignored completely. If you want to apply DateTime formatting you need to pass in an instance of a DateTime type.
Your first fix should be to change your parameter to be of type DateTime and not string. Otherwise a caller could pass "HI THERE" and your method/application would break, or worse push that value to a store and now your store is polluted with invalid value(s).
public void SomeMethod(DateTime daydate)
{
var dateOnly = dayDate.Date;
}

You should pass dayDate as a DateTime value instead of as a string. Then the format will work correctly.

Related

Convert string to date format in azure data factory using set variable

I have string date in set variable "20211222"
And I want to convert it into date like 2021-12-22.
And I have use this function in variable set dynamic content
#formatDateTime('20211222', 'yyyy-MM-dd')
But error occur In function 'formatDateTime', the value provided for date time string '20211222' was not valid. The datetime string must match ISO 8601 format
Is there any other function to convert this string "20211222" into date?
Actually the string '20211222' is already in the unambiguous format of YYYYMMDD and will always be interpreted this way. If you need to use this string input as a date, just do a cast:
SELECT CAST('20211222' AS date); -- 2021-12-22
If you wanted to formerly go from your text input YYYYMMDD to a text output of YYYY-MM-DD, then make a round trip:
SELECT CONVERT(varchar(10), CAST('20211222' AS date), 120);
Function formatDateTime expects "a string that contains the timestamp".
Example:
formatDateTime('03/15/2018 12:00:00', 'yyyy-MM-ddTHH:mm:ss')
You would have to manage to input in a timestamp format. The default format for the timestamp is "o" (yyyy-MM-ddTHH:mm:ss:fffffffK), which complies with ISO 8601 and preserves time zone information.
Please use the below logic:
#concat(substring(pipeline().parameters.Test,0,4 ),'-',substring(pipeline().parameters.Test,4,2),'-',substring(pipeline().parameters.Test,6,2))

Why does Date serialize for a JSON key differently than it does for a JSON value?

With the following code:
def date = new Date()
println new groovy.json.JsonBuilder([(date): date]).toString()
The result is something like
{"Fri Oct 28 15:00:45 ART 2016":"2016-10-28T18:00:45+0000"}
I was expecting the same representation as key and as value for the same date.
Can I force the JsonBuilder to output the keys with the same format as the values?
Thing is, JsonBuilder will format dates using by default a new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ") and I understand this is not what you wish to change. Since the "key" part is serialized with the toString() method, you have two solutions: either use [date.format("yyyy-MM-dd'T'HH:mm:ssZ"): date] or use metaProgramming to overload Date.toString() (it will be used for every Date object, though, so you might not want that).

Converting UTC Date to GMT in Groovy / Java

I am working with SoupUI a i need to adjust a Date/time (UTC) that i get back in a response to a GMT Date/time. The date that i get back in the respone looks as followes:
2012-11-09T00:00:00+01:00
I would like to convert this to
2012-11-08T23:00:00Z
Unfortunatly i lack Java skils and therefore also Groovy skils to be able to do this on my own. i did a lot o searches on date convertions but until now i was still unable to find what i was looking for. i will keep searching. if i do manage to get the solution then i will post it here.
Assuming there isn't a colon in the timezone portion, I believe this should work:
// Your input String (with no colons in the timezone portion)
String original = '2012-11-09T00:00:00+0100'
// The format to read this input String
def inFormat = new java.text.SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssZ" )
// The format we want to output
def outFormat = new java.text.SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss'Z'" )
// Set the timezone for the output
outFormat.timeZone = java.util.TimeZone.getTimeZone( 'GMT' )
// Then parse the original String, and format the resultant
// Date back into a new String
String result = outFormat.format( inFormat.parse( original ) )
// Check it's what we wanted
assert result == '2012-11-08T23:00:00Z'
If there is a colon in the TimeZone, you'll need Java 7 for this task (or maybe a date handling framework like JodaTime), and you can change the first two lines to:
// Your input String
String original = '2012-11-09T00:00:00+01:00'
// The format to read this input String (using the X
// placeholder for ISO time difference)
def inFormat = new java.text.SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssX" )

convert string to datetime in ado.net

I am trying to save data into my database using a vb form. I am using a datetimepicker to insert the date into my database. Here's an example
saveCommand.Parameters.AddWithValue("A_BOOKDATE", abookdatePicker1.Text).DbType = DbType.DateTime
In my database i set its attribute to time ,now the thing is i used the same line of code on another database and it worked but this one is giving this error
'Failed to convert parameter value from string to datetime'
How do i convert this string to datetime.
Many Thanks
Use DateTimePicker.Value instead of DateTimePicker.Text:
saveCommand.Parameters.AddWithValue("A_BOOKDATE", abookdatePicker1.Value) _
.DbType = DbType.DateTime
(If you ever actually have to convert a string to a DateTime, use DateTime.TryParseExact or DateTime.ParseExact, but in this case it's better not to use the string representation at all.)
You could also try to use
Convert.ToDateTime Method (String) (this one will return a datatime object or throw and exception)
DateTime.TryParse Method (String, DateTime) (this one returns the datetime as an output parameter, returns null on failure)

DateTime Hex Format

I'm getting a hex string when converting a SPListItem's date.
eg. "0x01c9d2be|0x809a8800"
This question answers it.
However, what's the proper way to convert it, what format is the DateTime object in?
What if I want to set the datetime? Would I have to convert it into hex format and assign it as a hex string?
There has to be a better and ideal way to extract modify the DateTime object.
Any ideas?
Thanks.
I'm able to extract the date and time. That's fine.
So when you are modifying the DateTime of a ListItem, you just simply assign it as a DateTime format and it will interpret it correctly; no need to generate the hex that it returns.
A DateTime value in .NET can also be represented as a 64-bit value. The strange string format on DateTime fields on SharePoint list items is a variant of such a 64-bit value encoded as two 32-bit hex values. I have found no documentation from Microsoft on this format. But by trial and error I found the following conversion method:
string[] words = strVal.Split('|');
int high = int.Parse(words[0].Substring(2), System.Globalization.NumberStyles.HexNumber);
uint low = uint.Parse(words[1].Substring(2), System.Globalization.NumberStyles.HexNumber);
long ticks = high;
ticks = ticks << 32;
ticks += low;
DateTime t = new DateTime(ticks);
t = t.AddYears(1600);
where t holds the result.
Another solution can be found here. It has something to do how Office 2007 stores dates..
SPListItem.Properties DateTime Fields are in weird Hex format

Resources