Convert UTC datetime to DateTimeoffset in MVC4

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Convert UTC datetime to DateTimeoffset in MVC4



I have date as string in the below format:


string s = "Thu Aug 22 00:00:00 UTC+0530 2013";



I have to convert this to DatetimeOffset, I have used Parse, ExactParse method. However, I couldn't seem getting it correct. I am getting String was not in correct format exceptions.


DatetimeOffset


Parse


ExactParse


String was not in correct format



I have to store the above value in database as datetimeoff object.


datetimeoff



Ex output: 2013-08-22 00:00:00 +05:30.


2013-08-22 00:00:00 +05:30.



Dates rendered from jQuery:


$.widget("df.datetime", $.df.datetimecontrols, {
_createInput: function () {
var min = this.element.attr("data-minRelDate"),
max = this.element.attr("data-maxRelDate");
debugger;
this._Input = $("<input>")
.addClass("datetime")
.attr("disabled", this._getDisableProp() ? "disabled" : "")
.prop("disabled", this._getDisableProp() ? true : false)
.addClass(this._getDisableProp() ? "disabled" : "")
.datetimepicker({
numberOfMonths: 2,
minDate: min,
maxDate: max,
//Uncomment below line for date format.
//dateFormat: $.datepicker.RFC_1123,
timeText: "Current time:",
hourGrid: 2,
minuteGrid: 5,
timeFormat: "hh:mm TT",
onSelect: $.proxy(this._change, this),
beforeShow: $.proxy(this._focusHndlr, this, 4),
onClose: $.proxy(this._focusHndlr, this, -4)
//TimeZone is not supported Across the browsers.To do manually there will change in the
// years(save light day etc.,) https://github.com/timrwood/moment/issues/162
})
.focus($.proxy(this._inputFocus, this))
.blur($.proxy(this._inputBlur, this))
.appendTo(this._Wrapper);

//Base element value to be widgets value.
if ($(this.element).val() != "") {
// If we wont specify time on recreate then time sliders will stay unchanged.
// we manipulate datepicker value and value of input to display differently.
// LLLL--> Thursday, April 18 2013 1:20 PM
// L --> 04/18/2013
// LT --> 8:30 PM
this._Input.datepicker("setDate", new Date(moment($(this.element).val()).format("LLLL")));
this._Input.val(moment($(this.element).val()).format("L LT"));
}
},





Define what is correct here. In another words, tell us what's desired output and what you get instead.
– Leri
Jul 1 '13 at 11:29





updated ques..thanku
– mmssaann
Jul 1 '13 at 11:33





Formatted your question feel free to revert back changes if context is lost.
– Leri
Jul 1 '13 at 11:37




1 Answer
1



Try this:


DateTimeOffset.ParseExact("Thu Aug 22 00:00:00 UTC+0530 2013",
"ddd MMM dd HH:mm:ss "UTC"zzz yyyy",
CultureInfo.InvariantCulture);



EDIT:
The basic way to deal with your formats. You can use regex or if you can determine your format in advance you can use a flag like "type".


public DateTimeOffset universalParser(string inputDate, int type)
{
switch (type)
{
case 1:
return DateTimeOffset.Parse(inputDate,
CultureInfo.InvariantCulture);

case 2:
return DateTimeOffset.ParseExact(inputDate,
"ddd MMM dd HH:mm:ss "UTC"zzz yyyy",
CultureInfo.InvariantCulture);
}
//if there is another type
return DateTimeOffset.Parse(inputDate);
}



Or you can override the DateTimeOffset.ParseExact method, with your own signature.



Better to create your own class types for each formats, and these can implement the same interface with different implementation of your universal parse method. And your code can runtime decide which implementation should be used (Dependency Injection).





This is working fine. but sometimes I am getting the datetime string as "Thu, 13 Jun 2013 00:00:00 GMT", how can I convert this to datetimeoffset, i dont know offset here?
– mmssaann
Jul 1 '13 at 12:07







@mmssaann How about normalizing your data? Choose what's your desired format, year in the middle of string or at the end.
– Leri
Jul 1 '13 at 12:10





In that case you have to write your universal parser, where you deal with all of your formats.
– speti43
Jul 1 '13 at 12:12





I dont have any specific place to deal the formats. Please refer to my other ques: stackoverflow.com/questions/17403792/…. If I change the value in screen I am getting the value as "Thu Aug 22 00:00:00 UTC+0530 2013". If I dont change and click on submit, I am getting it as "Thu, 13 Jun 2013 00:00:00 GMT". By the way we are rendering the dates from datetime picker in jquery, i will update the jquery script in ques.
– mmssaann
Jul 1 '13 at 12:20





jquery script is added to question
– mmssaann
Jul 1 '13 at 12:21






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Keycloak server returning user_not_found error when user is already imported with LDAP

PHP parse/syntax errors; and how to solve them?

How to scale/resize CVPixelBufferRef in objective C, iOS