在 C 中发送日期时遇到无效错误#

Invalid Error encountered while sending date in C#

提问人:hud 提问时间:11/9/2022 更新时间:11/9/2022 访问量:133

问:

我创建了一个 api,我正在向第三方发送一些 json 格式的数据。但是有一个列名是我得到的RFEDates

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

那么,有什么方法可以将该日期转换为正确的日期格式。(dd-MM-yyyy)

下面是我的代码。

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();
        }

    }        
C# asp.net 休息 日期时间

评论

0赞 Fildor 11/9/2022
这应该意味着“Fri Jan 12 1968 20:06:43 GMT+0000”吗?然后是毫秒格式的 Unix 时间。

答:

0赞 SachiJ 11/9/2022 #1

你试过这个吗?

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

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

评论

0赞 hud 11/9/2022
将错误获取为String not recognized as valid Datetime
0赞 SachiJ 11/9/2022
CultureInfo 提供程序 = CultureInfo.InvariantCulture;字符串 dateString = “-62135596800000”;字符串格式 = “ddMMyyyy”;DateTime 结果 = DateTime.ParseExact(dateString, format, provider);
0赞 SachiJ 11/9/2022
试试上面的一个?
0赞 SachiJ 11/9/2022
是负值吗?"-62135596800000" ?
0赞 hud 11/9/2022
是的,它的负值,但仍然得到同样的错误
0赞 keipala 11/9/2022 #2

该整数自纪元时间以来以微秒为单位。我不知道接受该格式的方法,但您可以创建一个新方法(在我的示例中)从 API 结果中提取整数值。实现该方法超出了本问题的范围,但之后您可以将其转换为所需的格式,如下所示:"\/Date(-62135596800000)\/"GetIntegarFromApiResult

int MicroSecsFromApiInput = GetIntegerFromApiResult(ObjIp.RFEDates);
long milliSecs = (long)MicroSecsFromApiInput / 1000;
DateTime date = DateTimeOffset.FromUnixTimeMilliseconds(milliSecs).UtcDateTime;
string formatted = date.ToString("dd-MM-yyyy");

评论

0赞 hud 11/9/2022
这是从哪里来的?GetIntegerFromApiResult
0赞 keipala 11/9/2022
这是您可以自己实现的方法。请参阅此 stackoverflow 答案作为示例。但是,您可能需要先转换为字符串。"\/Date(-62135596800000)\/"