Date format in asp.net MVC - c#

I have created a simple application build using asp.net MVC and SQL Server targeting the azure platform. My question is around date formatting. I have added a date picker which correctly uses dd/mm/yyy format. however when I pick the date I get the following validation error.
The project was build using database first. here is a screenshot of the property for that data item:
Where do I change the validation to accept dd/MM/yyyy. Also is there a way to set it so if there is a user in the US it will change date picker and validation to MM/dd/yyyy?
in both situations the date will be saved to the database as standard yyyy-MM-dd hh:mm:ss

Burn this into your brain:
mm is minutes.
MM is months.

The issue is clearly with your date format. You are entering dates in the format of dd/MM/yyyy while the system is expecting MM/dd/yyyy.
Please check in your model whether you have specified any specific date formats.
Change the Ui culture of Kendo
Like this
<script>
//set current to the "en-GB" culture script
kendo.culture("en-GB");
</script>

Related

Get Timezone based on Selected place

In my MVC application I am selecting the company & doing my operations based on that.
as you can see in the Image below::
Here the problem is with the Timezone based on the company I have selected. Because I have to Insert the Datetime for the current Timezone.
We know in C# there are Inbuild properties like ::
DateTime.Now
DateTime.UtcNow
TimeZone.CurrentTimeZone
I just want the time based on companies selected from the above Dropdown in C#.
Please Help me on this,
Thanks in Advance.
You can get it by using Date.getTimezoneOffset(). This links might helps you.
http://aspdotnet-naresh.blogspot.in/2013/05/aspnet-how-to-convert-to-different-time.html
get client machine timezone in asp.net mvc
http://www.w3schools.com/jsref/jsref_getTimezoneOffset.asp
Get the default timezone for a country (via CultureInfo)
To start, I recommend http://nodatime.org, because they have some mapping functions from the more common Olson-format to the windows time zone format. Read more about this: Olson Time Zone Id to Windows Standard Format using Noda Time
To solve your problem, there are multiple options:
Save the name of the timezon in Olson format in your database.
Save the geolocation in your database. You can use the shape file with timezones to select the right timezone based on geolocations. http://efele.net/maps/tz/world/. There is also library to read this file: http://www.easygisdotnet.com/api/

Date time implementation based on culture mvc4

I have an application which I developed in the UK. I have now deployed this to a US server and the dates are incorrect. I have a date time picker which I have set the format to be dd/mm/yyyy, when I select the date time picker I notice that when I inspect the element the date is actually 12/10/2013.
When I store this to the database this is being transformed as 2013-12-10 and then when I retrieve this from the database it is actually 10/12/2013.
Also when I call DateTime.Now in the code this is coming back as a US format (mm/dd/yyyy). I need everything to be consistent. How?
I have tried setting the culture info in the web config <globalization uiCulture="en-GB" culture="en-GB" /> but this is having no effect, some areas i am storing the date in sql server as GETDATE() and sometimes this is passed in as DateTime
I don't actually think you're dealing with incorrect behavior, you're most likely dealing with different TEXT representations of the SAME underlying DateTime or DATE item:
If Visual Studio is running on a system with en-US regional settings, it's insepectors will obviously use those settings when displaying dates in the debugger.
If you're looking at an SQL column with a data type of DATE, SQL might use the "SQL" standard representation for string, and that's why you see "2013-12-10"
When you're using Object Inspector to inspect the DateTime value selected into a control, Visual Studio is again using the system's settings to display that date. It can't show you the actual binary representation.
The crust of the problem:
Make sure you differentiate your actual date (stored in a DateTime variable or a DATE database column) from it's TEXT representation.
Make sure you get the correct date.
Make sure your application's users see the date in the expected format.
Learn to deal with the text representations of date that only you as a developer would see (the SQL-style representation and the Visual Studio inspector representation).

Time is not right in online version

On my website i show people the time when they open my page. So when they opened it at 4/29/2013 10:09 AM it wil show: 4/29/2013 10:09:14 AM
This all is working fine on my localversion.
But now i have my website online and the time is showing with 5 minutes delay.
I opened it at: 4/29/2013 10:10 AM it shows me: 29-4-2013 10:05:19
Who can help me with showing the right time online.
The time i want to show needs to be the local time.
The code:
<td>Capture Time:</td> // label
<td>#DateTime.Now.ToString()</td>
The time you are showing is server time ... and you are expecting it to be the same as local time.
If your server is in a different timezone than your client machine, the time shown will be different.
You may want to consider using javascript and do this on the client instead.
Or show the server time in a standard format like UTC and the client can try to determine the difference from local time.
DateTime.Now will show you server datetime. If your server & you are located in same timezone, then I think your local system time is different from server datetime.
Also, if your server is in different timezone then you should consider converting in your timezone before showing datetime on the UI.
Convert Time in Different TimeZones in ASP.NET
This will take server side date and time. If the server is in USA and if you are in India then you will get 12 hours difference. To solve this create a hidden input field and then wire a Javascript routine to the onsubmit event for the form. This routine would populate the hidden field with the time on the client machine.
The hidden field can used with ASP.NET by using the HTML control "HtmlInputHidden" class. You just give you input control a runat="server" attribute like any other server side control.
The server can then read out this time when the form posts back. You could even wrap this up in a server control if you need to do this in a number of places.
var now = new Date();
now.format("dd/M/yy h:mm tt");
Copy this date time to a hidden field which have runat="server" property and take that value from server.
Looks like it's displaying in a different format (dd-MM-yyyy while you want MM/dd/yyyy). Make sure your culture is set correctly in the web.config on your production server:
<system.web>
<globalization requestEncoding="utf-8" responseEncoding="utf-8"
culture="en-US" />
</system.web>
Also, try specifying the format in the ToString method:
DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt");
This will display the date in the format you want. The time, however, is dependent on the server's time and won't necessarily match the time on your local machine.

Publishing website project, database date format will be the same or converted to the default host date format?

I'm developing a web application and I'm using a gridview and sqldatasource.
I have some columns with type : Date.
In my control panel I set the format of the date for my country wich is : dd.mm.yyyy
In this case , the Table Data of the database in c# will have the
format for date : dd.mm.yyyy
I'm planing to publish my website on a host when I'll finish the project.
But my question is :
If in my database the columns with Date are in format dd.mm.yyyy , if I publish the database and use it with the website on the host , the dates will be dd.mm.yyyy or converted to the default format of the host?
The dates stored in the database do not have a certain format. You chose, in your control panel, to show them with the format dd.mm.yyyy, but that doesn't change what's really stored in the DB. They are similar to DateTime objects, representing a Date, not a string representing a date through a certain format. How the dates are turned into strings depends on what culture settings are used when converting it to a string, which in most cases (e.g. DateTime.ToShortDateString()) should be the current culture. If your code specifically said to use the format dd.mm.yyyy, then that will need to change.
So yes, it should be shown in the default format of the host.
(Side note: in .NET date-to-string formatting, MM is month, and mm is minutes. You shouldn't need to deal with this in this case, but take care that you use the correct one any time you do.)

system argument out of range exception

I have a report choosing start date and end date.
I tested at the office and it is working fine.
When it is installed at the user's place, the following error pops up.
I wonder why is that?
I bring back the database and run at the office.
Still working fine.
Well, it's not a parsing problem in DateTime.Parse, which is what I first expected. Your code (Form1.Calculat) is calling the constructor directly, so you should be able to log what values you're trying to use to create the DateTime, along with which row of the database is causing the problem.
We can't really do any of that diagnostic work for you, but once you've worked out what the values are, you should look through your code to work out where they're coming from.
Does your client have a different default culture to your development machine? That's normally the first port of call - but unless you're manually parsing date/time strings into their constituent bits, I wouldn't have particularly expected this failure mode.
If you could post some code, that would really help.
Are you passing the values to the TimeToTicks method ?
Probably these values are not forming a valid DateTime. Why it works on your office PC might be because you have different culture settings.
For example, in one culture "11/25/2010" is a valid date as the format (MM/dd/yyyy), but on a different culture, where date format is set as (dd/MM/yyyy) it will not be a valid date.
You can change the system date format from the control panel or modify your code accordingly. Hope this helps.
I think it is most of a problem Culture specific rather than any other issue. At one system the date format would be MM/DD/YYYY and at other system it would DD/MM/YYYY or something like that. So the datetime object should be picked culture specific and values should be passed that way as well.
Hope it fixes the problem.

Categories