ASP.NET MVC3 datetime format - asp.net-mvc-3

I have MVC3 project with Entity.
I have Data Annotation on DateTime property with DisplayFormat(DataFormatString="{0:dd/MM/yyyy hh:mm:ss}")
Controller with method which has entity as argument and calling view with same entity object, which displaying its date property.
So, I open browser and enter link http://site/entity/method?dateproperty=01/09/2012 01:02:03 (1 September 2012) and it displays 09/01/2012 01:02:03 (9 January 2012).
In view I'm using DisplayFor it's displaying like dd/MM/yyyy as I have a set DisplayFormat.
Problem is that it's reading like MM/dd/yyyy instead of dd/MM/yyyy, any solution?
P.S. I have tried globalization in web.config <sysmte.web> but didn't help.

I suspect the problem is that you haven't specified ApplyFormatInEditMode = true in your attribute, so it's parsing using the default format, but formatting with the format you're specifying.
I'm not an MVC dev by any means, but if you want the same format to be used in both directions, I believe you can just change your attribute to:
[DisplayFormat(ApplyFormatInEditMode = true,
DataFormatString="{0:dd/MM/yyyy HH:mm:ss}")]
Note that I've changed hh to HH, as I suspect you want a 24-hour format. I'd actually suggest using yyyy-MM-ddTHH:mm:ss (ISO-8601) as a less ambiguous format for the URL parameter - with a more user-centric display format, of course.

Related

Laravel Nova: Datetime with format and pickerFormat combined does not work

I got the fallowing date time field:
DateTime
::make('foobar')
->format('DD-MM-YYYY HH:mm:ss') // https://momentjs.com/docs/#/parsing/string-format/
->pickerFormat('d-m-Y H:i:S') // https://flatpickr.js.org/formatting/
->rules('required', 'date_format:Y-m-d H:i:s')
->firstDayOfWeek(1)
Momentjs does not recognise the date. I get a warning after changing the date in console:
Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged and will be removed in an upcoming major release. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.
It seems like momentjs is not getting the format? But I am clearly setting it in the nova resource and so it should get passed to vue, and so on? right?
Removing ->pickerFormat() allows me to save the date, but without displaying it the way I want in the picker.
I might be doing it completely wrong, so if someone could produce a working example of a DateTime filed with format and pickerFormat then that would be great as well.
edit:
Some more info. I re-transpiled Nova's using dev mode so I could poke around using Vue (chrome extension) tool.
I have also looked at the DateTime (vue) component without success. To bad it is closed source.
Use the package lathanhvien/novaofdatetime I just wrote recently. Hope it’s good to you!
Package link: https://packagist.org/packages/lathanhvien/novaofdatetime

How to display Timestamps as per User's location in cshtml

Hi I am using Razor as template for my .net applications.
Currently I am displaying time by #DateTime.Now.ToString("f").
Please help me how can I display date time specific to end user's zone in razor
use this keyword
local time depends on where the computer is located. The universal time is independent of this
#DateTime.UtcNow;
TimeZone zone = TimeZone.CurrentTimeZone;
// Demonstrate ToLocalTime and ToUniversalTime.
DateTime local = zone.ToLocalTime(DateTime.Now);
DateTime universal = zone.ToUniversalTime(DateTime.Now);

DateTime conversions using NodaTime on ASP.Net MVC 3 Razor website. How to?

I have the Date/Time stored in the database on the London's time zone (not UTC).
What I need is to retrieve this Date/Time, convert it to UTC and display considering the user's time zone (which they define when registering to the site - ie: en-GB, en-US, etc).
My first question is more related to the MVC structure, as I'm totally new to this (am a WebForms developer) - Where should I put this conversion code/class/helper? Model? ViewModel?
The second question is the conversion itself - I've played around with the NodaTime test project but can't find a proper way to do this conversion.
Any help is much appreciated.
Thank you in advance.
Jon Skeet may need to correct me, as I'm assuming this is correct in nodatime:
// Timezone data provider (inject with DI)
IDateTimeZoneProvider timeZoneProvider = DateTimeZoneProviders.Tzdb;
// London Timezone (can keep this as a singleton, since it'll be used often)
var londonTimeZone = timeZoneProvider["Europe/London"];
// Get your date/time from the database and map it as being local to London timezone
var yourDateFromDb = new DateTime(2013, 01, 23, 21, 00, 00); // This is what you'll get back from your database (although you may get back a DateTimeOffset)
ZoneLocalMapping zonedDbDateTime = londonTimeZone.AtLeniently(LocalDateTime.FromDateTime(yourDateFromDb)); <-- This is your date time with the correct offset (taking into account DST etc.)
// Map the London zoned date/time to the users local date/time
var usersTimezoneId = "Europe/Paris"; // <-- Store this value in users profile/db
var usersTimezone = timeZoneProvider[usersTimezoneId];
var usersZonedDateTime = zonedDbDateTime.WithZone(usersTimezone);
Assert.That(usersZonedDateTime.Hour == 22);
You should probably be aware that during timezone transitions (autumn clock change), you may get 2 possible dates for the zonedDbDateTime. The code here just gets the first or earliest date/time.
Edit: Updated code snippet with changes suggested by Jon Skeet
you can write some extension method like this :
public static DateTime ConvertToUtc(this DateTime d)
{
//do conversin and return it
}
and you will be able to call it like #Model.MyDate.ConvertToUtc() from any view you wish as long as you have the namespace of the ConvertToUtc inclucded on the top of page.
and to do the conversion see this pages
Convert DateTime from UTC to a user provided time zone with C#
http://www.dotnetcurry.com/ShowArticle.aspx?ID=593
How to work with time zones in ASP.NET?

Dateformat Rails 3.1

I've been banging my head against the table for a while now trying to figure out how to get dates in the format of dd/mm/yyyy to be successfully saved to the database.
I'm using the latest version of Rails (3.1.1) and have added;
Time::DATE_FORMATS.merge!(:default => '%d/%m/%Y')
Date::DATE_FORMATS.merge!(:default => '%d/%m/%Y')
To my environment.rb file and have also download the en-AU.yml locale file and have set the locale correctly. In my model I have;
validates :invoice_date, :presence => true
But everytime I submit a form with a date in the format of dd/mm/yyyy (24/11/2011 for example), it always says "Invoice date can't be blank". If I remove that validation rule it saves a null value. What do I have to do to get this work?
Thanks in advance :)
UPDATE:
Form field code is:
<input id="invoice_invoice_date" type="text" size="30" name="invoice[invoice_date]">
It's really quite strange. It's a nil value if it's out of the range of a mm/dd/yyyy format, however the trace is showing the inputted date... I'm at a loss.
I would suggest that you use the i18n api: http://guides.rubyonrails.org/i18n.html. So you can set the default time format for the particular locale in the locale file: example: en.yml. That might work for you.

ASP.NET MVC 3 not accepting my German date format settings - why?

I have a new ASP.NET MVC 3 project, and MVC and Razor are all brand new to me... can't figure this one out.
I have a dev machine on US-English Win7 x64, running English versions of VS 2010 Pro SP1 and ASP.NET MVC 3. But the app I'm working on is for a local client, and needs to be all in German and use all the formatting and defaults commonly used here.
In my view model, I have defined a variable of type DateTime and augmented it with some extra hints as to how to display (and edit) it - I need the Swiss-German date formatting, which is: day.month.year (in that order, separated by dots . - not slashes)
public class MyViewModel
{
[Display(Name = "Gültig am")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString="dd.MM.yyyy", ApplyFormatInEditMode=true)]
public DateTime ValidAt { get; set; }
}
(of course, there are more properties in that view model, but those aren't relevant just now)
In my Razor view, I simply use the default stuff set up by the ASP.NET MVC 3 Add View T4 template:
#Html.EditorFor(model => model.ValidAt)
Trouble is: if I enter something like 13.06.2011 (13th of June, 2011) which I believe is valid and conforming to my display and editing formats, I am still getting a client-side validation saying that The value '13.06.2011' is invalid - WHY???
There must be something really fundamental (and probably totally silly) that I'm missing here....
Even though I have set all of this up,
What do you use for client validation? Did you look at Combining validation methods in jQuery Validation plugin with "or" instead of "and" ?

Resources