Home > Net >  Invalid Error encountered while sending date in C#
Invalid Error encountered while sending date in C#

Time:11-09

I have created a api where I am sending some json format data to third party. But there is one column name as RFEDates whose I am getting as

"RFEDates":"\/Date(-62135596800000)\/" .

So is there any way where I can convert that date into proper date format in (dd-MM-yyyy) format.

Below is my code.

public void CallMasterService(IPCOLO_BLOCKCHAIN_FIELDS ObjIp, out string Msg, out string ErrorMsg)
    {
        Msg = "";
        ErrorMsg = "";
        NEDataBlockChain Tobj = new NEDataBlockChain();
        NEDataBlockChain[] ObjArrayNEdata = new NEDataBlockChain[1];

        if (string.IsNullOrEmpty(ObjIp.R4GSTATE))
        {
            Tobj.R4GState = "ABC";
        }
        else
        {
            Tobj.R4GState = Convert.ToString(ObjIp.R4GSTATE);
        }

        if (string.IsNullOrEmpty(ObjIp.CMP))
        {
            Tobj.CMP = "CMPState";
        }
        else
        {
            Tobj.CMP = Convert.ToString(ObjIp.CMP);
        }

        if (string.IsNullOrEmpty(ObjIp.VENDOR_CODE))
        {
            Tobj.SAPVendorCode = "ABC1234";
        }
        else
        {
            Tobj.SAPVendorCode = Convert.ToString(ObjIp.VENDOR_CODE);
        }

        if (string.IsNullOrEmpty(ObjIp.IP_COLO_SITEID))
        {
            Tobj.IPColoSiteId = "IN-123456";
        }
        else
        {
            Tobj.IPColoSiteId = Convert.ToString(ObjIp.IP_COLO_SITEID);
        }

        if (string.IsNullOrEmpty(ObjIp.SAP_ID))
        {
            Tobj.SAPId = "I-AB-ABCD-ENB-I111";
        }
        else
        {
            Tobj.SAPId = Convert.ToString(ObjIp.SAP_ID);
        }

        if (string.IsNullOrEmpty(ObjIp.INFRA_PROVIDER))
        {
            Tobj.InfraProvider = "Test";
        }
        else
        {
            Tobj.InfraProvider = Convert.ToString(ObjIp.INFRA_PROVIDER);
        }

        if (string.IsNullOrEmpty(ObjIp.SITE_NAME))
        {
            Tobj.SiteName = "Test";
        }
        else
        {
            Tobj.SiteName = Convert.ToString(ObjIp.SITE_NAME);
        }

        if (string.IsNullOrEmpty(ObjIp.SITE_ADDRESS))
        {
            Tobj.SiteAddress = "Test";
        }
        else
        {
            Tobj.SiteAddress = Convert.ToString(ObjIp.SITE_NAME);
        }

        if (string.IsNullOrEmpty(ObjIp.TOWER_TYPE))
        {
            Tobj.TowerType = "Test";
        }
        else
        {
            Tobj.TowerType = Convert.ToString(ObjIp.TOWER_TYPE);
        }

        if (string.IsNullOrEmpty(ObjIp.TOWER_TYPE))
        {
            Tobj.TowerType = "Test";
        }
        else
        {
            Tobj.TowerType = Convert.ToString(ObjIp.TOWER_TYPE);
        }

        string strRFCsDate = DateTime.Now.ToString("dd-MM-yyyy", CultureInfo.InvariantCulture);

        if (string.IsNullOrEmpty(ObjIp.RFCDATE))
        {
            Tobj.RFCDates = strRFCsDate;
        }
        else
        {
            Tobj.RFCDates = Convert.ToString(ObjIp.RFCDATE);
        }


        if (string.IsNullOrEmpty(ObjIp.ID_OD_COUNTCHANGE))
        {
            Tobj.ID_OD = "Test";
        }
        else
        {
            Tobj.ID_OD = Convert.ToString(ObjIp.ID_OD_COUNTCHANGE);
        }

        if (string.IsNullOrEmpty(ObjIp.DG_NONDG))
        {
            Tobj.DG_NonDG = "Test";
        }
        else
        {
            Tobj.DG_NonDG = Convert.ToString(ObjIp.DG_NONDG);
        }

        if (string.IsNullOrEmpty(ObjIp.EB_NONEB))
        {
            Tobj.Eb_NonEB = "Test";
        }
        else
        {
            Tobj.Eb_NonEB = Convert.ToString(ObjIp.EB_NONEB);
        }

        Tobj.status = "Active"; // site status

        if (string.IsNullOrEmpty(ObjIp.CITY_NAME))
        {
            Tobj.City_Name = "Test";
        }
        else
        {
            Tobj.City_Name = Convert.ToString(ObjIp.CITY_NAME);
        }

        if (string.IsNullOrEmpty(ObjIp.NEID))
        {
            Tobj.NEID = "INRJRJKRXXXXTW6001";
        }
        else
        {
            Tobj.NEID = Convert.ToString(ObjIp.NEID);
        }

        if (string.IsNullOrEmpty(ObjIp.FACILITY_LATITUDE))
        {
            Tobj.FacilityLATITUDE = "10.22";
        }
        else
        {
            Tobj.FacilityLATITUDE = Convert.ToString(ObjIp.FACILITY_LATITUDE);
        }

        if (string.IsNullOrEmpty(ObjIp.FACILITY_LONGITUDE))
        {
            Tobj.FacilityLONGITUDE = "90.50";
        }
        else
        {
            Tobj.FacilityLONGITUDE = Convert.ToString(ObjIp.FACILITY_LONGITUDE);
        }

        if (string.IsNullOrEmpty(ObjIp.RJ_STRUCTURE_TYPE))
        {
            Tobj.RJ_STRUCTURE_TYPE = "ENODEB";
        }
        else
        {
            Tobj.RJ_STRUCTURE_TYPE = Convert.ToString(ObjIp.RJ_STRUCTURE_TYPE);
        }

        if (string.IsNullOrEmpty(ObjIp.RJ_JC_NAME))
        {
            Tobj.RJ_JC_NAME = "Test";
        }
        else
        {
            Tobj.RJ_JC_NAME = Convert.ToString(ObjIp.RJ_JC_NAME);
        }

        if (string.IsNullOrEmpty(ObjIp.RJ_JC_CODE))
        {
            Tobj.RJ_JC_CODE = "12345";
        }
        else
        {
            Tobj.RJ_JC_CODE = Convert.ToString(ObjIp.RJ_JC_CODE);
        }

        if (string.IsNullOrEmpty(ObjIp.COMPANY_CODE))
        {
            Tobj.CompanyCode = "Test";
        }
        else
        {
            Tobj.CompanyCode = Convert.ToString(ObjIp.COMPANY_CODE);
        }

        if (string.IsNullOrEmpty(ObjIp.POLITICAL_STATE_NAME))
        {
            Tobj.POLITICAL_STATE_NAME = "Test";
        }
        else
        {
            Tobj.POLITICAL_STATE_NAME = Convert.ToString(ObjIp.POLITICAL_STATE_NAME);
        }

        if (string.IsNullOrEmpty(ObjIp.POLITICAL_STATE_CODE))
        {
            Tobj.POLITICAL_STATE_CODE = "1234";
        }
        else
        {
            Tobj.POLITICAL_STATE_CODE = Convert.ToString(ObjIp.POLITICAL_STATE_CODE);
        }


        //Web Api logic starts here

        ObjArrayNEdata[0] = Tobj;
        string apiUrl = ConfigurationManager.AppSettings["WebApiUrl"].ToString();

        var inputIPColoMaster = new
        {
            NEdata = ObjArrayNEdata,
        };

        string json = "";
        string inputJsonIPColoMaster = "";
        try
        {
            inputJsonIPColoMaster = (new JavaScriptSerializer()).Serialize(inputIPColoMaster);
            WebClient client = new WebClient();
            client.Headers["Content-type"] = "application/json";
            client.Encoding = Encoding.UTF8;
            json = client.UploadString(apiUrl, inputJsonIPColoMaster);
            DataTable dtRes = new DataTable();
            string GISSendDate = System.DateTime.Now.Date.ToString("dd-MM-yyyy", new System.Globalization.CultureInfo("en-US"));
            StringReader sr = new StringReader(json);
            dtRes = JsonConvert.DeserializeObject<DataTable>(json);

            CommonDB ObjDB = new CommonDB();

            if (dtRes != null && dtRes.Rows.Count > 0)
            {
                MasterServiceResponse MSRobj = new MasterServiceResponse();

                GlobalVariables.WriteMessageInfoInLogFile("IPCOLOMaster : "   "SAPID: "   ObjIp.SAP_ID   " Response Code "   dtRes.Rows[0]["response-code"].ToString()   " Response Message"   dtRes.Rows[0]["response-message"].ToString());
            }

            if (dtRes != null && dtRes.Rows.Count > 0)
            {
                CommonDB objDB = new CommonDB();
                ObjIp.BLCHAIN_RESP_MSG = Convert.ToString(dtRes.Rows[0]["response-message"]);
                ObjIp.BLCHAIN_RESP_CODE = Convert.ToString(dtRes.Rows[0]["response-code"]);

                CommonDB.UPDATE_BLOCK_CHAIN_INFO(ObjIp);
            }
        }
        catch (Exception ex)
        {
            GlobalVariables.WriteMessageInfoInLogFile_Error("Error - Invalid  : "   inputJsonIPColoMaster   "Ex Message :"   ex.Message   " Service response "  json);


            Msg = "1";
            ErrorMsg = ex.Message.ToString();
        }

    }        

CodePudding user response:

Have you tried this one?

DateTime dt = DateTime.ParseExact(yourObject.ToString(), "dd-MM-yyyy", CultureInfo.InvariantCulture);

Or

DateTime date = new DateTime(<>); string formatted = date.ToString("dd-MM-yyyy");

CodePudding user response:

That integer is in microseconds since epoch time. I do not know of way to accept that "\/Date(-62135596800000)\/" format, but you can create a new method (GetIntegarFromApiResult in my example) that pulls the integar value from the API result. Implementing that method is outside the scope of this question, but after that you can convert that to your required format like below:

int ApiInput = GetIntegerFromApiResult(ObjIp.RFEDates);
long microSecs;
long.TryParse(ApiInput, out microSecs);
long milliSecs = (long)microSecs / 1000;
DateTime date = DateTimeOffset.FromUnixTimeMilliseconds(milliSecs).UtcDateTime;
string formatted = date.ToString("dd-MM-yyyy");
  • Related