DateTime string format (dd.MM) localized XAML - datetime

I found some usefull question but they didn't helped me. I'm developing Windows Phone 8 app and I want to show only dd.MM on TextBlock (DateTime binded) if culture is Turkish. If culture is en than display MM.dd
Text="{Binding MyProperty.CreateDate, StringFormat=\{0:dd.MM\}}"
Above code runs true in Turkish culture. But in English it's meaningless. Actually problem is not with the English or turkish. I just want to show Day and Month formatted in current culture.

If you want to specify your own format, you can create different format strings in your localization resource files and bind to StringFormat
Text="{Binding MyProperty.CreateDate, StringFormat={StaticResource shorDateFormatString}}"
Alternatively, set the date text in the codebehind, with some custom logic to see whether the month or the day comes first in the local convention
string shortDatePattern = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern
if(shortDatePattern.indexOf("dd") < shortDatePattern.indexOf("MM"))
dateLabel.Text = MyProperty.CreateDate.toString("dd.MM")
else
dateLabel.Text = MyProperty.CreateDate.toString("dd.MM")
As suggested in this response

Related

Date conversion on Live Server issue

I have a web application that when working in development is fine, but going to Live it does not work.
What the situation is to do with dates. A user enters in a date in the UK format through a text, and I use DateTime.ParseExact(txtDate.Text, "dd-MM-yyyy", Nothing) to tell the date datatype that the date must remain in UK style. As I say, everything is fine in development, but when it goes to Live, it says the that the string is not a valid datetime format.
I have tried Convert.ToDateTime, and all sorts of things, but it just won't work!
Anyone have any suggestions?
I suspect that the culture setting on the live server is different to that of the dev machine.
From the docs:
If provider is a null reference (Nothing in Visual Basic), the current culture is used.
So you need to specify the InvariantCulture try this:
DateTime.ParseExact(datetimeString, "dd/MM/yyyy",
System.Globalization.CultureInfo.InvariantCulture)

asp.net label currency format

Hi how do format my label to a currency format.
code
lblFundsAvail.Text = result.Data.FundsAvail.ToString();
thanks
Try using the format specifier C:
lblFundsAvail.Text = result.Data.FundsAvail.ToString("C");
Note though that this will use whatever currency the server happens to have as its current culture, and this may not be the currency you want. You can specify another culture as the second parameter.

Conversion from string "31/03/2012" to type 'Date' is not valid

My web app is running perfectly in asp vb.net editor. But when i run my web app through IIS7 then i get this error. What am i missing in configuring IIS7? Is there anyone who can suggest something?
Thanks in Advance
Because your IIS7 is configured for the English Language and that date is probably Italian or something similar. You'll have to tell to the Date.Parse which culture to use.
Something like
dateValue = Date.Parse(yourDate, CultureInfo.CreateSpecificCulture("it-IT"))
Or you can change the culture in your IIS7
Here there are the instructions
for example if you use the UI
Using the UI Open IIS Manager and navigate to the level you want to
manage. (omissis)
In Features View, double-click .NET Globalization.
On the .NET Globalization page, in the property sheet, click to select
the global setting you want to edit, and select a value from the
drop-down list.
In the Actions pane, click Apply.
Or you could set the culture of your app in the web.config
<system.web>
<globalization culture="it-IT" uiCulture="it-IT"/>
</system.web>
If you are sure that the date is always in exactly that format, then you can use ParseExact instead:
var date = DateTime.ParseExact(
"31/03/2012",
"dd/MM/yyyy",
System.Globalization.CultureInfo.InvariantCulture);
You can also use the CDate function to parse the date.
Dim dDate As Date = CDate("31/03/2012")
The advantage of using this function over the DateTime parsing functions is that you can feed it any acceptable format of date string and it will convert it. It will throw an error if it can't parse the date.

Displaying proper date format depending on culture

I am using a control for a popup calendar date picker. This uses a javascript function, SetText, to set the textbox to the given date. I can't change anything in the calendar control itself but I can override the SetText function. The SetText javascript just takes the TextBox name and the date value in string format and sets the TextBox to the string.
The problem:
I need to display the date in the format "April 30".
Easy to do. Use getMonth() and getDate() where I can parse the information from there.
Now, I need to make sure this shows correctly for different cultures. For example, the UK shows dates as "30 April". Since the code-behind(c#) could be sending the date in the UK format how do I know in the javascript that they're using UK(dd/mm/yyyy) and not US(mm/dd/yyyy)?
The browsers navigator language could be set to one setting while the server is set to another to cause a January 4 as April 1 mismatch.
You are using the Microsoft Ajax Framework, this framework defines a set of "client-side type extensions" which provide added functions or "extensions" to the JavaScript base types.
The Date Type Extensions is what you're looking for, specifically the Date.parseLocale function.
With this function you can parse a string, using a given format.
You can synchronize your server-side and client-side culture by setting the ScriptManager.EnableScriptGlobalization property to true, and use the Date.parseLocale function without specifying any format.
Give a look to this article:
Walkthrough: Globalizing a Date by Using Client Script
See toLocaleString and related functions.
If you control the backend, why not just send a timestamp and push it into Date object?
As for formatting on the client side, since I was already using Dojo, I solved this problem by using dojo.date.locale.format. It was completely painless.
Locale is detected automatically or can be set arbitrarily.
Shorthand format options (e.g.: long short)
Data selectors (e.g.: time, date)
Ability to specify an arbitrary date/time pattern (probably not application to this application, but still useful).
Tutorial: http://docs.dojocampus.org/dojo/date/locale
API doc:
http://api.dojotoolkit.org/jsdoc/1.3/dojo.date.locale.format
Date format descriptions: http://www.unicode.org/reports/tr35/tr35-4.html#Date_Format_Patterns
Three things you could use:
1) toLocaleString - As suggested already. The problem with this is when sending a string of "4/1/2009" this can result in a couple things. January 4 or April 1.
2) navigator.language and navigator.systemLanguage - After you get the date string you can check to see what language the system is in and parse the date from there. The problem with this and solution 1 is what if you have a UK server and the browsers machine is US. You will have the code behind sending April 1 as 1/4/2009 where the javascript will read the string as whatever language the clients browsers is. So, UK server and US browser will give you a wrong result.
3) Use Code Behinds Culture - Create a variable in your javascript that when the page loads, it will call a function in your code behind that returns this.Page.Culture from there, you will know what culture the string is being sent back as. This will eliminate the mismatch that the first two solutions can cause. It will take a little extra work to make sure it's displayed correctly but at least you will be able to use the string without having the possibility of mismatching cultures.
toLocaleDateString would be a better solution than toLocaleString for your problem as it doesn't include the time (as you only are requesting the date).
The open-source JavaScript library Date.js has some great methods for formatting dates, as well as it supports a bunch of languages:
Date.js at Google Code: http://code.google.com/p/datejs/
If you want nicely formatted dates / times, you can just pass a formatting string (nearly identical to those used in .NET Framework) into any Date object's .toString() method.
It also has a whole set of cultures which allow you to simply include the appropriate script for that culture.
If you want to manage that yourself (as we do in our apps), you can find resources which give you the list of appropriate resource strings for a given culture. Here's one that shows proper formatting strings for a ton of cultures: http://www.transactor.com/misc/ranges.html
As you are using ASP.NET then you may also be using ASP.NET Ajax. If so, there are two properties on the ScriptManager that are of use to you:
EnableScriptLocalization - Gets or sets a value that indicates whether the ScriptManager control renders localized versions of script files.
EnableScriptGlobalization - Gets or sets a value that indicates whether the ScriptManager control renders script that supports parsing and formatting of culture-specific information.
<asp:ScriptManager ID="AjaxManager" runat="Server" EnablePartialRendering="true"
EnableScriptGlobalization="true" EnableScriptLocalization="true" />
When you enable both of these (set to true) then ASP.NET Ajax extenders etc. should automatically be localised into the culture specified in web.config:
<configuration>
<system.web>
<globalization
fileEncoding="utf-8"
requestEncoding="utf-8"
responseEncoding="utf-8"
culture="en-GB"
uiCulture="en-GB" />
</system.web>
</configuration>
For instance, setting this will localise the AjaxControlToolkit Calendar into your specificed culture.
Even if you are NOT using ASP.NET Ajax adding a ScriptManager and enabling localisation will give you a useful javascript variable called __cultureInfo that contains a JSON array of localised formate, such as currencies, dates etc.
"CalendarType":1,"Eras":[1],"TwoDigitYearMax":2029,"IsReadOnly":true},"DateSeparator":"/","FirstDayOfWeek":1,"CalendarWeekRule":0,"FullDateTimePattern":"dd MMMM yyyy HH:mm:ss","LongDatePattern":"dd MMMM yyyy","LongTimePattern":"HH:mm:ss","MonthDayPattern":"dd MMMM","PMDesignator":"PM","RFC1123Pattern":"ddd, dd MMM yyyy HH\u0027:\u0027mm\u0027:\u0027ss etc....
I solved this problem by using Datejs as
In codebehind(aspx.cs) I get the culture for an employee and add the appropriate js to the header as
string path =
"http://datejs.googlecode.com/svn/trunk/build/date-"
+ GetCulture() + ".js"; Helper.AddJavaScript(this, path);
(in your case you can get the culture from navigator.systemLanguage (or navigator.browserLanguge etc) and add a script tag to the header with src attribute pointing to the appropriate path)
On the client-side I use
d.toString(Date.CultureInfo.formatPatterns.shortDate)
where d is any date object
(I tried using Date.today().toShortDateString() but it was throwing exception. (the CultureInfo JSON object had a different structure than what the function expects).

The string was not recognized as a valid datetime

objfile.dateFileDate=convert.ToDatetime(Format(txtdate.text,"MM/dd/yyyy hh:mm"))
following error is coming
The string was not recognized as a
valid datetime .There is an unknown
word starting at 0.
What should i do to save this datetime,
please help
You can't format normal text using datetime formats.
Try
C#
objfile.dateFileDate=DateTime.ParseExact(txtdate.text, "MM/dd/yyyy hh:mm", null);
VB.NET
objfile.dateFileDate=DateTime.ParseExact(txtdate.text, "MM/dd/yyyy hh:mm", Nothing)
This is assuming dateFileDate is a DateTime type and that the txtdate.text is in the above format.
If your program is used by a international crowd, read on :)
ppl from different cultures will write dates in diffrent formats, so if your always going to parse the string that could be get sticky. Consider using the calander control?
Im saying this based on personal experience.
Also finding out why your current one is failing, i would do a DateTime.Now.ToString() and compare that to whats in the textbox so you can see whats curently being typed in wrong ( While debugging off course, to help track down the problem)
Try hh:nn instead of hh:mm
I believe mm is Months in two digit format and nn is minutes in two digit format.

Resources