默认的MVC模板使用“ @ DateTime.Now.Year”来显示版权年份,但是我宁愿到处都使用NodaTime。
我目前正在使用Ninject将IClock的实例注入到执行时间或日期特定内容的Controller中。是否有建议的方式来访问MVC中类似于“ DateTime.Now”的“全局IClock”?我想我可以将IClock注入到每个Controller中,然后将其传递到每个视图中,但是有时可以访问全局对象会很不错。
我知道我可以在Layout模板中使用SystemClock.Instance ...而是引用一个全局的,可测试的IClock会更好。
我想要获得当月的最后一天和下个月的最后一天.
这是我到目前为止提出的代码,但是我被迫在NodaTime代码和.NET方法DateTime.DaysInMonth()之间混合以实现我正在寻找的东西,这听起来不对.
class Program {
public static void Main(string[] args)
{
DateTimeZone mst = DateTimeZoneProviders.Tzdb["MST"];
Instant now = SystemClock.Instance.Now;
ZonedDateTime mstNow = now.InZone(mst);
LocalDate mstLastDayOfCurrentMonth = new LocalDate(mstNow.Year, mstNow.Month, DateTime.DaysInMonth(mstNow.Year, mstNow.Month));
Console.WriteLine("Last Day of Current Month: {0}", mstLastDayOfCurrentMonth);
//move into the next month
LocalDate nextMonth = mstLastDayOfCurrentMonth.PlusDays(1);
LocalDate mstLastDayOfNextMonth = new LocalDate(nextMonth.Year, nextMonth.Month, DateTime.DaysInMonth(nextMonth.Year, nextMonth.Month));
Console.WriteLine("Last Day of Next Month: {0}", mstLastDayOfNextMonth);
}
}
Run Code Online (Sandbox Code Playgroud)
你能否告诉我NodaTime推荐的方式来获得当前和下个月的最后一天?
提前致谢
假设我有678天,如何计算从那一刻起有多少年,几个月和几天?
Duration duration = Duration.FromStandardDays(678);
Instant now = SystemClock.Instance.Now;
Instant future = now + duration;
// I have to convert NodaTime.Instant to NodaTime.LocalDate but don't know how
Period period = Period.Between(now, future);
Console.WriteLine("{0} years, {1} months, {2} days", period.Years, period.Months, period.Days);
Run Code Online (Sandbox Code Playgroud) 我即将疯狂处理日期时间问题和网络.
我在中央时区托管了一个Web服务器.当东部时区的客户使用我的应用程序尝试和安排某一天的项目时,他们会传递(例如)2015年3月14日的值.当我们将代码传递回我们发送到web api的模型时,我们坚持使用类似下面的代码.
moment.utc($("#mydatepicker").val).hour(0).minute(0).second(0)).toISOString();
Run Code Online (Sandbox Code Playgroud)
这会产生如下字符串:
2015-03-14T04:00:00.000Z
Run Code Online (Sandbox Code Playgroud)
在web api中将项目转换回服务器时,它将转换为
3/13/2015 11:00:00 PM
Run Code Online (Sandbox Code Playgroud)
逻辑然后剥离时间,你可以看到从这里发生的事情.由于我剥离了时间,现在是前一天,这是持久存储到数据库的值.
我需要知道从某个时刻发送值的一些方法,最好是作为客户端时区的ZonedDateTime进入web api.然后,我可以将其转换为UTC以保持数据库中的持久性.
我已经看到了使用NodaTime.Serialization.JsonNet的事情,但我不清楚如何将它与Moment一起使用并在web api/ajax中来回传递.
在我们的ASP.NET MVC 5应用程序中,我们拥有带有时区ID(IANA)的用户配置文件.
public class User
{
public int Id { get; set; }
public string TimeZoneId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
任务是在当地时间早上6点向所有用户发送电子邮件.我们的计划是每小时向所有时区发送电子邮件,当地时间是当时时间早上6点(根据服务器的UTC时间计算).
由于我们只有时区ID,我希望得到相应的时区ID,比方说,偏移-4:00:00 - 考虑到DST.
string[] timezoneIds;
Run Code Online (Sandbox Code Playgroud)
然后,我可以像这样查询我的数据库:
db.Users.Where(x => timezoneIds.Contains(x.TimeZoneId));
Run Code Online (Sandbox Code Playgroud)
我的问题显然是,这是一个体面的想法,还是我不知道这个问题的最佳做法?
谢谢.
在我的NinjectConfigurator中
container.Bind<IClock>().To<SystemClock>();
Run Code Online (Sandbox Code Playgroud)
我也试过了
container.Bind<IClock>().To<SystemClock>().InSingletonScope();
Run Code Online (Sandbox Code Playgroud)
但我得到这个错误:
错误激活
IClock使用结合IClock到SystemClock没有构造函数可用来创建实现类型的实例.激活路径:3)将依赖IClock注入SystemManager类型构造函数的参数时钟2)将依赖ISystemManager注入到AccountController类型构造函数的参数systemManager中1)对AccountController的请求
建议:1)确保实现类型具有公共构造函数.2)如果已实现Singleton模式,请使用与InSingletonScope()的绑定.
这是我的注入类,与我在项目中使用IoC的所有其他工作类相同:
private readonly IDateTime _dateTime;
private readonly IClock _clock;
public SystemManager(IDateTime dateTime, IClock clock)
{
this._dateTime = dateTime;
this._clock = clock;
}
Run Code Online (Sandbox Code Playgroud)
我找不到任何可以帮助我的事情.任何帮助深表感谢.
我正在尝试将NodaTime的OffsetDateTime类型映射到SQL Server,但我不确定如何解决NodaTime OffsetDateTime和SQL Server DateTimeOffset类型之间的阻抗.
我的主要问题是让LINQ支持正常工作,因为OffsetDateTime没有比较运算符,如<.系统在处理平等方面也有所不同.NodaTime考虑时间瞬间和偏移量,而SQL Server仅考虑时间瞬间.
2015-12-24 11:18+01:00而2015-12-24 10:18+00:00被认为是在SQL Server平等的,但不是NodaTime相等.
我考虑ICompositeUserType过将UTC日期/时间和偏移量存储在单独的列中(类似于SQL Server 2008之前),但OffsetDateTime没有UTC/Instant属性.因此,我无法看到如何让date.ToInstant()LINQ查询正确映射到中的属性ICompositeUserType.
我有dotnet核心应用程序,我正在研究这是跨平台的.我想在我的项目中使用Noda作为实用程序,但是即使我在项目中将nodatime定义为依赖项,也会出现以下错误:
System.IO.FileNotFoundException: Could not load file or assembly 'NodaTimestrong text, Version=2.0.0.0, Culture=neutral, PublicKeyToken=4226afe0d9b296d1'. The system cannot find the file specified.
File name: 'NodaTime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=4226afe0d9b296d1'
at project.ef.CalendarHelper.ValidateTimeZone(String timezoneTypeName)
at project.ef.CalendarHelper.
Run Code Online (Sandbox Code Playgroud)
以下是项目配置文件:
API项目
{
"buildOptions": {
"preserveCompilationContext": true,
"emitEntryPoint": true,
"warningsAsErrors": true,
"debugType": "portable",
"copyToOutput": {
"include": [
"config.json",
"Certificate.pfx"
]
}
},
"dependencies": {
"AspNet.Security.OAuth.Introspection": "1.0.0-alpha2-final",
"AspNet.Security.OAuth.Validation": "1.0.0-alpha2-final",
"Microsoft.AspNetCore.Diagnostics": "1.0.0",
"Microsoft.AspNetCore.Mvc": "1.0.0",
"Microsoft.AspNetCore.Mvc.Formatters.Json": "1.0.0",
"Microsoft.AspNetCore.Mvc.Cors": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
"Microsoft.Extensions.Configuration.CommandLine": "1.0.0",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.EntityFrameworkCore.Design": …Run Code Online (Sandbox Code Playgroud) 我有一个数据库,所有日期都是UTC,驱动程序总是发送给我的DateTime对象DateTimeKind.Unspecified.我想假设它们是UTC并转换为当地时间.
使用NodaTime实现这一目标的最简单方法是什么?
Instant.FromDateTimeUtc(utcDate).InZone(localZone); 继续抱怨DateTimeKind应该是Utc.
我正在尝试使用NodaTime来计算两个日期之间的小时数,我得到了这个例外:
"单位包含时间单位:小时.参数名称:单位"
此代码可以正常工作数年,数月和数天.
public ElapsedTimeNodaTime(DateTime StartDate, DateTime EndDate)
{
var birthday = new LocalDate(StartDate.Year, StartDate.Month, Date.Day);
var today = new LocalDate(EndDate.Year, EndDate.Month, EndDate.Day);
Years = Period.Between(birthday, today, PeriodUnits.Years).Years;
Months = Period.Between(birthday, today, PeriodUnits.Months).Months;
Days = Period.Between(birthday, today, PeriodUnits.Days).Days;
Hours = Period.Between(birthday, today, PeriodUnits.Hours).Hours;
}
Run Code Online (Sandbox Code Playgroud) nodatime ×10
c# ×6
ninject ×2
asp.net-core ×1
asp.net-mvc ×1
datetime ×1
javascript ×1
nhibernate ×1
timezone ×1