1

Closed

DateTime not included in DateTime field model

description

As described in the comments on this stack overflow answer the model for the Date time field as two string properties date and time, but you can't access the DateTime value which makes formatting the datetime difficult in your view.
Closed Jun 19, 2013 at 9:42 PM by BertrandLeRoy

comments

sebastienros wrote Jun 19, 2013 at 6:09 PM

Yes you can:

Model.ContentField.DateTime

RichardGarside wrote Jun 19, 2013 at 9:07 PM

Indeed you can. Thanks for answering the question too.

Please close this issue.

RichardGarside wrote Jun 20, 2013 at 3:31 PM

I think I'd like to reopen this issue.

The date string in the model is converted into the correct time zone like this

Date = value != DateTime.MinValue ? TimeZoneInfo.ConvertTimeFromUtc(value, Services.WorkContext.CurrentTimeZone).ToString("d", _cultureInfo.Value) : String.Empty

The DateTime in the ContentField is not converted and I'm currently getting all my days being shown as one day earlier. I didn't notice, but my client did. I'm in GMT + 1 at the moment.

I've added code to my view to do the conversion, but it would be nice to be able to access a raw Datetime object that was set to the current timezone and putting that in the model makes sense to me.

RichardGarside wrote Jun 20, 2013 at 3:33 PM

P.S. I'm only storing the date, which I guess is why I'm right on the edge of the day and the conversion to UTC puts me one day earlier.

sebastienros wrote Jun 20, 2013 at 7:01 PM

You need to convert it to the current timezone, or whatever time zone you want to use. This is not a bug.

There is also a shape to help for this:

@Display.DateTime(yourDateTimeUtcHere) And

@Display.DateTime(yourDateTimeUtcHere, yourCustomFormat)

Or

@Display.DateTimeRelative(yourDateTimeUtcHere)

RichardGarside wrote Jun 21, 2013 at 9:34 AM

I didn't think it was a bug, just a bit confusing. I thought adding the converted date to the model might be a nice enhancement.

However, using the helpers sorts everything, so is good.

I've found I needed to call the helper like this, or it threw an error.

@Display.DateTime(DateTimeUtc: Model.ContentField.DateTime, CustomFormat: T("dddd d MM yyyy"))