mmssaann
mmssaann

Reputation: 1507

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.

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

Ex output: 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"));
        }
    },

Upvotes: 0

Views: 1459

Answers (1)

speti43
speti43

Reputation: 3046

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).

Upvotes: 2

Related Questions