Technology: Visual Studio 2008 .NET, Winforms
bsTransactions.DataSource = Transactions.Tables[2];
bsnTransactions.BindingSource = bsTransactions;
txtTransOverrideDate.DataBindings.Add("Text", bsTransactions, "TransactionDate", true,DataSourceUpdateMode.Never, "", "MM/dd/yyyy");
Currently, I'm getting an error saying that the string wasn't a recognized DateTime string. Despite the fact that if I get the column type from
Transactions.Tables[2].Rows[0]["TransactionDate"].DataType.ToString();
returns "System.DateTime" and the actual value looks like "1/23/2010 12:00:00 AM"
I'm trying to format a string that currently looks like "1/23/2010 12:00:00 AM" to only show the date.
The catch is, there's a BindingSourceNavigator being use, so just applying the formatting to the textbox after the fact only applies to the first value, but not any others that are navigated to using the bindingNavigator.
I have a feeling that using the "bsTransactions" to applies bindings to the textbox, it's changing the datatype, hence it not being recognized as a DateTime.
txtTransOverrideDate.DataBindings.Add("Text", bsTransactions, "TransactionDate", true,DataSourceUpdateMode.Never, "", "MM/dd/yyyy");
I made changes in the original post to mimic this, but this is the answer.
Related
This is input format:
yyyy:MM:dd'T'HH:mm:ss'Z' (Coming as a string from json service)
Required output format:
dd-mmm-yyyy
I have tried with {{txnDate | date:'dd-mm-yyyy'}}
but it is not working..
What is the format you are following for your date?
A quick var a = new Date(); a.toISOString(); in console will give you something like "2015-02-19T13:30:13.347Z". The formatted string you are receiving is not following any standard and I am afraid parsing it to date will result in Invalid Date in most of the browsers.
So you can either
Get your Date in proper format.
Make the best use of whatever is available. You can use split to break your string into individual components.
Something like:
var a = "yyyy:MM:dd'T'HH:mm:ss'Z'" //Replace with actual string
b=a.split(':') will result in ["yyyy", "MM", "dd'T'HH", "mm", "ss'Z'"] giving you year and months in b[0] and b[1].
For date, you can use b[2].substring(0,2) to give you dd.
You have all date components(apart from time components, which you don't need anyway) as string.
Either use them directly(as a string) or make a date object using these components(since you want month in MMM format).
$scope.txnDate = new Date(b[0]+'/'+b[1]+'/'+b[2].substring(0,2));
I am sure there are more ways to optimize this. Comment if this doesn't work for you, will try to elaborate more.
For whatever reason the data I am getting from database is coming in this format: " 830AM"
Also notice that extra space before number 8
I need to format it to show as "08:30 AM" in Silverlight XAML or Styles.
How would we do that?
If you're sure that the incoming format is always the same, you could use DateTime.ParseExact to get the DateTime object -- then convert it back to a string with the format you want.
string timeStr = " 830AM";
string format = "h:mmtt";
DateTime time = DateTime.ParseExact(timeStr.Trim(), format, CultureInfo.InvariantCulture);
string timeStrFormatted = time.ToString("hh:mm tt", CultureInfo.InvariantCulture);
To do this in Silverlight XAML, you could use an IValueConverter in the binding. Better still, use a read-only property on the view-model, which performs the above conversion.
I'm making this simple webserver that can host my blog, but whatever I do; I can not execute a proper formatted time into my html/template.
Here's what I do:
I've created this struct:
type Blogpost struct {
Title string
Content string
Date time.Time
}
Next up I've created this little func that retrieves the blogposts with corresponding title/dates from the Appengine Datastore and return that as a slice:
func GetBlogs(r *http.Request, max int) []Blogpost {
c := appengine.NewContext(r)
q := datastore.NewQuery("Blogpost").Order("-Date").Limit(max)
bp := make([]Blogpost, 0, max)
q.GetAll(c, &bp)
return bp
}
Finally, in the blogHandler I create a slice based on the retrieved data from the Appengine Datastore using:
blogs := GetBlogs(r, 10)
Now when I Execute my template called blog like this, the dates of the blogs are being parsed as default dates:
blog.Execute(w, blogs) // gives dates like: 2013-09-03 16:06:48 +0000 UTC
So, me, being the Golang n00b that I am, would say that a function like the following would give me the result I want
blogs[0].Date = blogs[0].Date.Format("02-01-2006 15:04:05") // Would return like 03-09-2013 16:06:48, at least when you print the formatted date that is.
However that results in a type conflict ofcourse, which I tried to solve using:
blogs[0].Date, _ = time.Parse("02-01-2006 15:04:05", blogs[0].Date.Format("02-01-2006 15:04:05")) // returns once again: 2013-09-03 16:06:48 +0000 UTC
It is probably some n00b thing I oversaw once again, but I just can't see how I can't override a time.Time Type in a slice or at least print it in the format that I want.
While I looking for a similar functionality to simply format and render a time.Time type in a html/template, I fortuitously discovered that go's template parser allows methods to be called under certain restrictions when rendering a time.Time type.
For example;
type Post struct {
Id int
Title string
CreatedOn time.Time
}
// post is a &Post. in my case, I fetched that from a postgresql
// table which has a datetime column for that field and
// value in db is 2015-04-04 20:51:48
template.Execute(w, post)
and it's possible to use that time in a template like below:
<span>{{ .CreatedOn }}</span>
<!-- Outputs: 2015-04-04 20:51:48 +0000 +0000 -->
<span>{{ .CreatedOn.Format "2006 Jan 02" }}</span>
<!-- Outputs: 2015 Apr 04 -->
<span>{{ .CreatedOn.Format "Jan 02, 2006" }}</span>
<!-- Outputs: Apr 04, 2015 -->
<span>{{.CreatedOn.Format "Jan 02, 2006 15:04:05 UTC" }}</span>
<!-- Outputs: Apr 04, 2015 20:51:48 UTC -->
As a note; my go version is go1.4.2 darwin/amd64
Hope it helps others.
Your Date field has type time.Time. If you format it as a string, and parse it back, you'll once again get a time.Time value, which will still print the default way when the template execution calls its String method, so it's not really solving your problem.
The way to solve it is by providing the template with the formatted time string itself instead of a time value, and you can do that in multiple ways. For example:
Add a method to your blog post type named FormattedDate or similar, which returns a string properly formatted in the style of your preference. That's the easiest, and probably the nicest way if you don't have a fancy use case.
Add a string field to your blog type named FormattedDate or similar; that's very similar to the above option, but you have to be careful to set and update the field whenever necessary, so I'd prefer the method option instead.
If you'd like to format time values in multiple ways within the template, you might also opt to define a template formatter function, so that you might do something like {{post.Date | fdate "02-01-2006 15:04:05"}}. See the documentation on Template.Funcs, the FuncMap type, and this example for details on how to do that.
Update: Sample code for the first option: http://play.golang.org/p/3QYdrDQ1YO
If you output a time.Time in a template it will be converted to a string. This default conversion is what you see. If you need a different format you have two possibilites:
Add a `FormatedDate string field to your Blogpost and populate it from Date via Date.Format(whatever)
Write and register a formatting filter` in your template (see http://golang.org/pkg/html/template/#Template.Funcs) and use this.
I'm working with sql server report builder and I'm trying to change the value of an image according to a chosen date from a parameter.
I've selected external as source and in the expression window I have something like this:
=iif(Parameters!Date.Value <> 11.04.2013, "http://rack.0.mshcdn.com/media/ZgkyMDEyLzEyLzA0L2I1L3doZXJlZG9nb29nLmJoTi5qcGcKcAl0aHVtYgk5NTB4NTM0IwplCWpwZw/4931e287/304/where-do-google-doodles-come-from--ff2932470c.jpg", " ")
but I receive the following message:
Argument not specified for parameter 'TruePart' of 'Public Function IIf(Expression As Boolean, TruePart As Object, FalsePart As Object) As Object'.
I'm pretty new to work with the report builder, so please someone help me find out the solution.
I pasted your expression into a sample report set up with a parameter Date, data type Date/Time, and got the same error when using the expression.
I resolved this by changing the data being compared to the parameter in the expression into a string that can readily be converted to a date, so from 11.04.2013 to "11-Apr-2013":
=iif(Parameters!Date.Value <> "11-Apr-2013", "http://rack.0.mshcdn.com/media/ZgkyMDEyLzEyLzA0L2I1L3doZXJlZG9nb29nLmJoTi5qcGcKcAl0aHVtYgk5NTB4NTM0IwplCWpwZw/4931e287/304/where-do-google-doodles-come-from--ff2932470c.jpg", " ")
This stopped the error occurring for me and worked as expected when I selected different dates; i.e. the URL on all dates except 11-Apr-2013.
I know there are numerous posts on this issue but none worked for me and hence I thought to post one by myself. I hope I am not spamming.
All I want to do is to convert string into a DateTime object. It worked fine on VS Web Develpoment Server but when I published it to IIS it started throwing the exception "String was not recognized as a valid DateTime." I went through numerous posts and tried following code but they all failed. For simplicity I hard coaded the DateTime string. Please note, format of DateTime in the string is fixed, I have to use that only.
Convert.ToDateTime("27-6-2012 9:05 PM")
// Just for the sake of it I also used CurrentUICulture and InstalledUICulture in the line below.
// Note: For me CurrentCulture = CurrentUICulture = InstalledUICulture = en-US
Convert.ToDateTime("27-6-2012 9:05 PM", CultureInfo.CurrentCulture)
DateTime.Parse("27-6-2012 9:05 PM")
// Just for the sake of it I also used CurrentUICulture and InstalledUICulture in the line below.
// Note: For me CurrentCulture = CurrentUICulture = InstalledUICulture = en-US
DateTime.Parse("27-6-2012 9:05 PM", CultureInfo.CurrentCulture)
// Just for the sake of it I also used CurrentUICulture and InstalledUICulture in the line below.
// Note: For me CurrentCulture = CurrentUICulture = InstalledUICulture = en-US
DateTime.ParseExact("27-6-2012 9:05 PM", "{0:d-M-yyyy h:mm tt}", CultureInfo.CurrentCulture)
Then just for experinment I changed format of the date in string to "6/27/2012 9:05 PM". I found all of the above worked except DateTime.ParseExact. For me if any one of the above codes work, I am good but the problem is that I cannot change the format of date in the string, that comes as a parameter which is not in my control.
This looks like such a trivial issue but I don't know what is it that I am missing? It is so embarrassing. Can anyone help me please?
Thanks in advance.
It's possible that the regional settings on the server are not the same as on your dev environment. Try this. http://support.microsoft.com/kb/241671
EDIT - Look in control panel under the regional settings. This will effect how the server reads dates.