I have a C# Web Service running on a Windows machine, and i have this code:
Details = tInfoRequestHistory.Select(i => new AppJsonDetails
{
Type = i.PlatformUserId == null ? "Request" : "Response",
Date = i.Date.ToString("D"),
Time = i.Date.ToString("hh:mm tt"),
Text = i.Comment,
}).ToArray()
And i'm receiving this JSON in my app:
{
Date = "lunes, 12 de enero de 2015";
Text = "402\\nGarcia\\n01/12/15 12:52 \\nBla bla";
Time = "11:52 ";
Type = Request;
}
As you can see i'm missing the PM/AM (in fact there is a blank space) in the node "Time" that is generated with this instruction i.Date.ToString("hh:mm tt")
If i execute my code in my development machine everything is working ok.
I'm using windows server 2012 and the last IIS 8.0 and i have just change the regional settings of my server to match my development machine so i'm reading in my server clock "12:00 PM"
I'm sure that this is some configuration i'm missing in the Server or in the IIS or maybe in the Web.config.
Any help or idea will be really appreciated.
Well, since you using DateTime.ToString() without any IFormatProvider, sounds like your CurrentCulture's AMDesignator or / and PMDesignator properties are empty string.
I would suggest a use InvariantCulture as a IFormatProvider like;
i.Date.ToString("hh:mm tt", CultureInfo.InvariantCulture);
Or change current culture that have non-empty AMDesignator and PMDesignator properties in your windows machine region settings.
Related
I have a .NET Core 2.2 web app running inside a docker-compose app. I'm saving different UTC time stamps to a database, like this:
//set asked state on question
Question questionInDb = c.Questions.Single(x => x.Id == id);
questionInDb.Asked = true;
questionInDb.AskTime = DateTime.UtcNow;
c.SaveChanges();
In the apppsettings.json I have a section for my app's settings, containing the locale the application is supposed to run on. I set the CultureInfo.CurrentCulture like so:
//get locale from settings, defaulting to de-DE
string locale = "de-DE";
locale = Configuration.GetValue<string>("AppSettings:Locale");
var ci = new CultureInfo(locale, false);
//actually setting locale
CultureInfo.CurrentCulture = ci;
I set up my Entity Framework to set all DateTimes to DateTimeKind.Utc, so the app knows the DateTimes it gets are all UTC.
Now, when displaying the time on a view, it's not the right hour. In the view.cshtml I'm using #q.AskTime.ToString("HH:mm:ss dd.MM.yy"), but it always returns the exact value from the database. It's supposed to show one hour more.
I even tried setting the container's timezone to CET (Centran Euopean Time) using ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone (in my docker-compose I set the environment variable TZ to Europe/Berlin).
I'm out of ideas.
Cultures (also known as locales) are used for displaying and formatting data and is a different concept than timezones. You can find and create a specific TimeZoneInfo from a timezone name and convert an UTC date and time using the ConvertTimeFromUtc() method.
Keep in mind that different operating systems use different timezone names. For example you can use Europe/Berlin for Linux and Central Europe Standard Time for Windows.
Example:
TimeZoneInfo tz;
try
{
// Linux
tz = TimeZoneInfo.FindSystemTimeZoneById("Europe/Berlin");
}
catch (TimeZoneNotFoundException)
{
try
{
// Windows
tz = TimeZoneInfo.FindSystemTimeZoneById("Central Europe Standard Time");
}
catch (TimeZoneNotFoundException)
{
// Fallback to UTC
tz = TimeZoneInfo.Utc;
}
}
var converted = tz.ConvertTimeFromUtc(DateTime.UtcNow);
I am using kendo datePickerFor
Code into model=====
[DataType(DataType.Date)]
[Display(Name = "Date:")]
[DisplayFormat(DataFormatString = "{0:MM-dd-yyyy}",
ApplyFormatInEditMode = true)]
public DateTime TransactionDate { set; get; }
Into view page====
#(Html.Kendo().DatePickerFor(m => m.TransactionDate)
.Format("MM-dd-yyyy")
.ParseFormats(new List<string>()
{
"MM-dd-yyyy"
})
)
Also use javascript====
$(function () {
kendo.culture("en-US");
})
My kendo DatePickerFor is inside a form.
when I submitting the form giving error - "The value '09-16-2016' is not valid for Date:."
My local machine date format is "dd-MM-yyyy".
When I set this format into datepicker,form submitted correctly.
But I want to set specific format which does no depend on local machine format
Please know me how to solved this problem.
Normally, you would want to match the cultures on the client and on the server:
http://docs.telerik.com/kendo-ui/aspnet-mvc/globalization#match-cultures
The server-side culture can be set, depending on the regional information provided in the browser's request.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Content_negotiation
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Language
If there is a valid reason not to do this, then you will have to manually take care of submitting the date value in the correct format, expected by the server.
I had similar problem - the best description I found (but not a solution) is here: https://blog.gerardbeckerleg.com/posts/kendo-ui-mvc-grid-datepicker-in-line-and-in-cell-edit-validation-error-the-value-is-not-valid/
My local machine culture date format is dd.MM.yyyy, in my multilanguage app I wanted to force always one date format: yyyy-MM-dd, so I added (to never show local date format)
#(Html.Kendo().DatePickerFor(m => m.TransactionDate)
.Format("yyyy-MM-dd"))
Problem occurred while saving changes only inside kendo grid when I used In-Line edition and only for my local machine culture, which does not allow format with month-first. When I input date in format yyyy-MM-dd, after accepting this change it somehow swapped month and day, and my date 2020-01-05- (january 5th) was saved as 2020-05-01 (may 1st). And when a day was > 12 same error as yours occured (e.g. for date 2020-12-31 - december 31st it couldn't change 31 info a month, and I got error: "The value 31.12.2020 is not valid for Date"
In error messsage a given format was exactly like my local machine culture (dd.MM.yyyy -> 31.12.2020) but error occured so the date was treated as MM.dd.yyyy and in this format 31.12.2020 is not valid indeed.
What was strange I noticed different behaviour when I ran same application from client machine (but same local culture as mine) and when I was debugging in VS on local machine (on a local machine I couldn't reproduce an error, only on client machine)
What I did?
I checked if I load proper kendo culture (I did): kendo.culture("pl-PL");
I changed kendo.culture.pl-PL.min.js date format info yyyy-MM-dd. To not change min.js file I did as an extension js method (notice the first true parameter!):
var customCulture = $.extend(true, kendo.culture(), {
calendars: {
standard: {
patterns: {
d: "yyyy-MM-dd",
g: "yyyy-MM-dd HH:mm",
G: "yyyy-MM-dd HH:mm:ss"
}
}
}
}
);
It forces takes given date formats and pushes (merges) them into currently loaded kendo.culture(), what does the trick. Because I always want to show yyyy-MM-dd format I is what I need.
The idea of solution was taken from here: https://www.telerik.com/forums/calendar-customization-764d6343160b
public JsonResult TimeValidation(string pickUp, string delivery)
{
var errorMessage = string.Empty;
var dateTime = DateTime.MinValue;
if (!DateTime.TryParse(pickUp, out dateTime))
errorMessage = "Invalid date";
if (!DateTime.TryParse(delivery, out dateTime))
errorMessage = "Invalid date";
}
4/29/2015 3:30:00 PM pickup from ie
4/30/2015 12:00:00 AM delivery from ie
4/29/2015, 3:30:00 PM pickup from firefox
4/30/2015, 12:00:00 AM delivery from firefox
its working good in chrome and firefox but its not converting to datetime in internet explorer 11 please obseve , between date and time
Assuming this is C# (looks like it is) and it is running on the server (not actually in the browser): You should check to see what the value of System.Globalization.CultureInfo.CurrentCulture is. See if it is different for a request coming from IE vs one of your other browsers. DateTime.TryParse(string, out DateTime) uses this value to help parse the string.
For instance, the date you provided: "28/04/2015 07:59:00" will cause TryParse to return false if the current culture is en-US, but if the current culture is es-MX, then it will return true.
I'm not sure why it would be different between browsers off the top of my head, but it's at least a place to start looking.
I came across a similar issue and the issue was the javaScript method
toLocaleDateString() in IE11 return string with some RTL characters! which results in invalid data and these characters aren't visible.
A simple fix using regex
toLocaleDateString().replace(/[^A-Za-z 0-9 \.,\?""!##\$%\^&\*\(\)-_=\+;:<>\/\\\|\}\{\[\]`~]*/g, '')
I tried the same regex at the back C# but it didn't work, however, I didn't want to spend more time on this so I just applied the front end solution.
More details, source
I have screen in c# asp.net webapplication, where i add news on particular dates.And can edit those dates also.It workes in my local sytem.But shows datetime error when it was running in iis 7 server(Used sql database).And i knew that the short date and long date format in server was different from local system.So i changed date format in local system same as in iis.But still it is working properly.
Instead of guessing culture settings write code that sets one you need before reading from database/restore after unsing Thread.CurrentCulture property. Simialr to code below (need to also use CurrentUICulture, chose cuture you need and wrap code around setting/restoring into try/finally for real code)
var oldCulture = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
// read from DB
...
Thread.CurrentThread.CurrentCulture = oldCulture;
I have two laptops both with windows xp sp3, vs 2010 and target framework .Net 3.5. When processing DateTime variable, I found that with laptop1
DateTime oldOrderDate;
string strnewdate = string.Empty;
CultureInfo provider = CultureInfo.InvariantCulture;
if (DateTime.TryParse(items[1], out oldOrderDate))
strnewdate = oldOrderDate.ToString("yyyy-MM-dd");
returned an exception "string was not recognized as a valid datetime" , but the codes below:
oldOrderDate = DateTime.ParseExact(items[1], "dd/MM/yyyy", provider);
strnewdate = oldOrderDate.ToString("yyyy-MM-dd");
works. OTOH, with laptop2,
oldOrderDate = DateTime.ParseExact(items[1], "dd/MM/yyyy", provider);
strnewdate = oldOrderDate.ToString("yyyy-MM-dd");
returned an exception "string was not recognized as a valid datetime" , but the code below:
if (DateTime.TryParse(items[1], out oldOrderDate))
strnewdate = oldOrderDate.ToString("yyyy-MM-dd");
works. So, my question is how I should do processing of a DateTime variable to work in both laptops. I'd really appreciate any advice you could give me. Thank's in advance.
I am not sure but look at the regional settings of date and time in control panel. In my systems they also differ and hence different results.