I'm probably blind to something right in front of me.
But In my MVC application, I have an object/model that has a DateTime property.
I do an AJAX GET to retrieve the model. The datetime when received looks something like Date(142342323).
I want to convert this date to the locale setting of the user. In moment.js I don't see a way to set it to local.
I thought about getting the currentculture in MVC and passing that as a value (and storing it as an hidden field on the page ) and then using that for the javascript date format...but there seems to be a discrepancy between c# formats and javascript formats.
Ideas anyone?
I mainly have 1 format for Europe and 1 for US (dd/mm/yyyy and mm/dd/yyyy).
Related
I have a Kendo UI grid that fetches data from API by AJAX call.
It has a column with date and the date is coming from backend in this format "2022-03-08T19:02:00".
This is one of the column of my grid where I am parsing the date :
{
field: "createdDate",
title: "Created Date",
template:"<div class='createdDateTemplate'>#= kendo.toString(kendo.parseDate(createdDate)) #</div>",
width: 150
}
I want to display this date in AEST or AEDT which ever is being followed . Is this possible from the template itself ?
EDIT :
I know that Kendo automatically renders date time in local format something like this for me:
GMT+1000 (Australian Eastern Standard Time)
But is it possible to display this time with +10 added to time itself ?
Edit: I am trying this
template:"<div class='createdDateTemplate'>#= kendo.toString(kendo.timezone.convert(kendo.parseDate(createdDate), 'Etc/UTC', 'Etc/GMT+10'), 'dd/MM/yyyy hh:mm tt') #</div>",
but I am getting this error:
Cannot read properties of null (reading 'getFullYear')
Kendo automatically converts the dates to the user's local time if you add zzz to the end of date , like this yyyy-MM-ddTHH:mm:sszzz.
One more thing , you say that your date is UTC but it doesn't have a Z (zero utc offset) at the end. Kendo won't add the offset without it , so I would recommend adding it.
Maybe you could try something like this:
#= kendo.toString(kendo.parseDate(modifiedDate+'Z','yyyy-MM-ddTHH:mm:sszzz'), 'dd/MM/yyyy hh:mm tt')#
Yes, you can display the date however you like it. Read the Kendo Date Parsing documentation. I would suggest though that the back-end send it in ISO format (e.g. yyyy-MM-ddTHH:mm:ss). So that on the front-end, you can just do parseDate without the need for additional parameters. Perhaps depending on the language setting of the browser it will be shown in AEST or AEDT format. If not, then you can do toString on the date.
I am sending ajax request and parameters in url one of which is date. The format in url looks like dd.mm.yyyy. When goes in controller DateTime parameter reads it as mm/DD/yyyy. How can I change this behaviour and tell mvc to read it as dd/MM/yyyy ?
Example:
URL: 12.01.2017
MVC: 01.12.2017
That depends on the configuration that you have on your browser for the culture, unless you POST the data in the format you want.
On IE,
Click Internet Options.
Click Languages, under the General tab.
Click Add...
Select the language you would like to add.
Click OK.
Then move the language that you want up to have the priority over the others, the Culture will come from there and the date format will change.
You have to set the format attribute in the constructor of your datetimepicker assuming you are using jquery.
For example:
$('.datepicker').datepicker({ "format": 'dd/mm/yyyy' });
In my SQL Server I have this date format: dd-mm-yyyy, and I have some dates matches to this format. But when I use a datagridview in my application, it parses as mm-dd-yyyy but show dd-mm-yyyy. So if I have 05.12.2012 in my database, it thinks that 05 is month so it shows as 12.05.2012.
I have tried to use
Application.CurrentCulture = new CultureInfo("tr-TR");
but it did not work.
I should change the parsing format of my application, not showing format. How can I do that?
Thank you.
You should refer to the 'SET DATEFORMAT' command to set the format of the date. However, I think you problem lies somewhere else. You should be able to access the date column as a datetime value in c#. Usually data is passed in it's native format and so you should not have problems like the one you describe.
You can also send your date in the same date format in sql from your application.
Convert your date to sql format using the culture.
This works for me as we are using different culture in one application.
I'm having an issue with date/time formats in ASP.NET/C#. I have my SQL Server database set up with a PostDate field set to a type of "datetime". But it's saving the date in a strange format. I added a new row through a form and I got this as the date/time string:
2012-09-28 14:56:48.910
When it gets parsed by JSON.NET it gets even stranger. I get:
2012-09-28T14:56:48.91
The date and time are obviously correct, but how do I set things so that I can parse the date into a human-friendly way? There isn't really any code to post because the date is being added when the row is inserted. I'd like to format this as "Sept. 28, 2012 2:56 pm". How do I do that? Do I need to format the string before or after it's parsed as JSON?
That's not a "strange" format at all. The second form is ISO-8601; the first is ISO-8601 without the T. Considering the strange formats you can get in JSON, it looks like you've been let off pretty lightly!
Serialization formats aren't meant to be user-friendly, particularly - they're meant to be machine-to-machine formats.
I would hope that JSON.NET would give you a DateTime after parsing; it should only be giving you the ISO-8601 format after you've converted back to JSON.
If you've got a DateTime that you want to format for user consumption, there are all kinds of options with standard and custom format strings. Don't forget that you should respect the culture of the user, as far as possible - so make sure you're taking appropriate steps to either set the thread's current culture to be the user's one, or that you're passing the culture explicitly to DateTime.ToString etc.
You can try it in C#:
.ToString("MMM d yyyy, h:mm tt")
I am wondering what is the best way to figure out how to convert a datetime?
Right now my computer is setup as dd/mm/yyyy yet I am taking date as mm/dd/yyy so when I try to do
DateTime.Convert();
DateTime.Parse();
DateTime.TryParse();
I either get nothing back or it crashes. Now I could tell it about the mm/dd/yyyy format and it probably would convert. However the problem is these dates are are coming from user uploaded files.
They could be in any format combination. I am trying to find ways to do it.
The problem I see is that I am looking at the dates in an uploaded file so I am not sure if looking say at the browser date format would help or not.
I am using asp.net mvc and I am trying to get a solution that handle date formats automatically so I don't have to ask the user about the date format they have (I figure the more things I have to ask them the less likely the user will continue on)
No, you can't figure out automatically what date-time format a user meant to use once the value is on the server. You need more information to parse it correctly (e.g. 1/2/3 can mean a lot of different dates depending on the culture).
Consider one of the following solutions:
Convert the entered date to a text representation in a standard format (i.e. ISO 8601 - 2012-02-09) using JavaScript on the client before you send it to the server. The code would look something like this: d.getUTCFullYear()+"-" + d.getUTCMonth() + "-" + d.getUTCDate().
Send the local culture information to the server along with date value to be converted and do the conversion on the server.
Force the user to enter the date in a specific format (e.g. Use 3 text boxes labeled "Month", "Day", and "Year" instead of one text box with free input).
chobo2 (I like the 'handle') :)
you can detect the locale culture and work on that at will. see the following SO Q/A for pointers:
Where is the system locale/culture set for .Net
the key is to NOT have to set anything in particular, but identify the locale and act accordingly.