[英]Hi, I am trying to run a web job in Azure app service but it is throwing Error related to Datetime
I running a webjob in Azure app service which will get the current datetime and do some operation.我在 Azure 应用服务中运行一个网络作业,它将获取当前日期时间并进行一些操作。 but this works fine when I run it in my local machine (I am creating a webjob using a.Net console application).Once I try to run it as Webjob then I am getting this error.但是当我在本地机器上运行它时效果很好(我正在使用.Net控制台应用程序创建一个webjob)。一旦我尝试将它作为Webjob运行,我就会收到这个错误。
Unhandled Exception: System.FormatException: String was not recognized as a valid DateTime.
System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
System.Convert.ToDateTime(String value)
for more details please find the code below有关更多详细信息,请在下面找到代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Net;
using System.IO;
namespace Zoho_API_Services
{
class Items
{
#region DateTime
public DateTime datetime { get; set; }
public static DateTime Current_time { get; set; }
public static DateTime Target_time { get; set; }
//Constructor to get current date time
public Items()
{
datetime = DateTime.Parse(DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss"));
}
#endregion
public static void Main()
{
//Call the constructor
Items obj = new Items();
//Initialize Current time and Target time
Current_time = obj.datetime;
Target_time = Current_time.AddMinutes(55);
if (Current_time >= Target_time)
{
//Reset the Current time and Target time
Current_time = obj.datetime;
Target_time = Current_time.AddMinutes(55);
// Do something
}
else
{
// Do Something
}
Console.ReadKey();
}
}
}
The error is thrown when constructor is being called.调用构造函数时会引发错误。 Please Help请帮忙
There are several problems with this line of code:这行代码有几个问题:
datetime = DateTime.Parse(DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss"));
DateTime.Now
uses the system's local time zone. DateTime.Now
使用系统的本地时区。 In the cloud this is UTC by default.在云中,默认为 UTC。
WEBSITE_TIME_ZONE
app setting, but only on Windows.由于您使用的是 Azure web 作业,您可以设置WEBSITE_TIME_ZONE
应用程序设置,但仅限于 Windows。 If you're running on Linux, this won't work.如果您在 Linux 上运行,这将不起作用。DateTime.UtcNow
and then convert to a specific time zone using TimeZoneInfo.ConvertTime
if necessary.更好的选择是使用DateTime.UtcNow
,然后在必要时使用TimeZoneInfo.ConvertTime
转换为特定时区。 In your string format, you are using hh
which is for hours 1-12 on a 12-hour clock.在您的字符串格式中,您使用的是hh
,即 12 小时制的 1-12 小时。 Thus, if the time is 13:00 or later, you will lose 12 hours when you parse it.因此,如果时间是 13:00 或更晚,解析时会损失 12 个小时。 HH
is for hours of a 24-hour clock. HH
表示 24 小时制中的几个小时。
In your string format, you are using dd/MM/yyyy
date format.在您的字符串格式中,您使用的是dd/MM/yyyy
日期格式。 That may be your format locally, but by default in Azure the CurrentCulture
is the InvariantCulture
, which uses MM/dd/yyyy
format.这可能是您在本地的格式,但默认情况下,在 Azure 中, CurrentCulture
是InvariantCulture
,它使用MM/dd/yyyy
格式。 You get the error because you're trying to parse a value like 15/04/2021
by treating 15 as a month, which is out of range.您收到错误是因为您试图通过将 15 视为一个月来解析像15/04/2021
这样的值,这超出了范围。
One should never ever create a string just to parse it again in the same code.永远不应该创建一个字符串只是为了在相同的代码中再次解析它。 There's absolutely no reason for that.绝对没有理由这样做。 If your intent was to truncate fractional seconds, you can do that with:如果您的意图是截断小数秒,您可以这样做:
datetime = datetime.AddTicks(-datetime.Ticks % TimeSpan.TicksPerSecond);
Putting it all together, you should probably be doing the following:综上所述,您可能应该执行以下操作:
// I chose the time zone based on the location in your Stack Overflow user profile.
TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("India Standard time"); // or "Asia/Kolkata" on Linux
// Get the UTC time and convert to the given time zone.
datetime = TimeZoneInfo.ConvertTime(DateTime.UtcNow, tz);
// Truncate to seconds.
datetime = datetime.AddTicks(-datetime.Ticks % TimeSpan.TicksPerSecond);
The above will avoid any impact of culture settings because there's no parsing or formatting going on.以上将避免文化设置的任何影响,因为没有进行解析或格式化。
Also, you should remove Console.ReadKey();
此外,您应该删除Console.ReadKey();
- It will hang your web job. - 它将挂起您的 web 作业。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.