草庐IT

Current_DateTIme

全部标签

c# - 让 datetime.now 返回到最近的秒数

我有一个“要求”将时间戳记为最接近的秒数……但并不比这更准确。四舍五入或截断时间都可以。我想到了这个可憎的事dateTime=DateTime.Parse(DateTime.UtcNow.ToString("U"));(U是长格式日期和时间。“2007年1月3日17:25:30”)是否有一些不那么可怕的方法来实现这一点?编辑:所以从链接的截断毫秒答案(感谢约翰奥多姆)我要这样做privatestaticDateTimeGetCurrentDateTimeNoMilliseconds(){varcurrentTime=DateTime.UtcNow;returnnewDateTime(c

c# - DateTime AddMinutes 方法不起作用

我的方法的目的是获取当前时间,并将其设置回20分钟。据我所知,我的方法是正确的,但输出显示了其他内容。这是我的代码:DateTimecurrentTime=DateTime.Now;doubleminuts=-20;currentTime.AddMinutes(minuts);Console.WriteLine("Nuværendetid:"+currentTime);输出结果如下:25-11-201114:01:54我的结果应该是:25-11-201113:41:54.谢谢! 最佳答案 描述AddMinutes函数返回一个Date

c# - 如何将字符串转换为 DateTime as UTC 就这么简单

假设我有这个字符串:如何将它转换为具有UTC时间的DateTimeOffset对象-意味着-00:00作为时区-即使我在特定时区的机器上运行它?假设字符串:“2012-10-08T04:50:12.0000000”Convert.ToDateTime("2012-10-08T04:50:12.0000000"+"Z");-->日期时间d={10/8/20126:50:12AM}我希望它成为日期时间d={10/8/20124:50:12AM}好像它会理解我想要的日期一样简单(顺便说一句-我的机器在时区+02:00) 最佳答案 使用Da

c# - LinqToSQL 错误 : Operation is not valid due to the current state of the object

在更新命令期间,我收到以下错误:Operationisnotvalidduetothecurrentstateoftheobject我试图从更新命令中删除一列并且它工作正常。此列是一个FK,与其他工作正常的FK相似。这是执行更新的代码:ti.NumeroTitolo=titolo.Numero;ti.RKTipoTitoloGenereTitolo=titolo.RkTipoTitoloGenereTitolo;ti.RKBanca=titolo.RkBanca;ti.DataScadenza=titolo.DataScadenza;ti.RKTipoEsito=titolo.RkTi

c# - 将 Xamarin Forms 中的 DateTime 格式化为设备格式字符串

在运行PCLXamarin.Forms项目并且我的部署目标包括iOS、Android和Windows时,如何将DateTime对象格式化为设备默认日期时间格式的字符串。根据此thread,DateTime.ToShortString()无法按照MSDN要求工作还有这个bug.是否有任何基于表单的解决方案,或者我是否需要从特定于平台的项目中获取它?对于Android,我可以使用DI从native项目执行以下操作:Stringformat=Settings.System.GetString(this.context.ContentResolver,Settings.System.DateF

c# - DateTime.ParseExact 具有 7 位数字/一位或两位数字的月份

到现在为止,我认为我会理解DateTime.ParseExact的工作原理,但这令人困惑。为什么下一行返回false?DateTime.TryParseExact("2013122","yyyyMdd",CultureInfo.InvariantCulture,System.Globalization.DateTimeStyles.None,outlastUpdate)月份也可以有两位数。我认为它应该能够理解它意味着2013年1月22日。为什么我走错了路?我是否遗漏了什么或者是否有简单的解决方法?与此同时,我正在使用这个不是很优雅但有效的解决方法:publicstaticDateTim

c# - 使用 Interlocked 的线程安全 DateTime 更新。*

我可以使用Interlocked.*同步方法来更新DateTime变量吗?我希望在内存中保留最后一次触摸时间戳。多个http线程将更新最后一次触摸DateTime变量。我很欣赏DateTime变量是被替换而不是更新的值类型。我能想到的最好的方法是将时间戳记作为一个long中的总滴答数classx{long_lastHit;voidTouch(){Interlocked.Exchange(ref_lastHit,DateTime.Now.Ticks);}} 最佳答案 选项1:使用带有Interlocked和DateTime.ToBin

c# - 扩展 ASP.NET 身份角色 : IdentityRole is not part of the model for the current context

我正在尝试在我的MVC5应用程序中使用新的ASP.NETIdentity,特别是我正在尝试将ASP.NETIdentity集成到现有数据库中。我已经阅读了有关DBFirst和ASP.NETIdentity的SO问题/答案,并且遵循了所有建议我仍然无法向我的数据库添加角色,尽管我在添加用户时没有问题。这是我的代码:varcontext=newPayrollDBEntities();varroleManager=newRoleManager(newRoleStore(context));boolroleExists=roleManager.RoleExists(roleDto.Name);

c# - 了解 TaskScheduler.Current 的行为

这是一个简单的WinForms应用程序:usingSystem;usingSystem.Diagnostics;usingSystem.Threading;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceWindowsFormsApplication{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privateasyncvoidbutton1_Click(objectsender,EventArgse){varts=Task

c# - 错误绑定(bind) Gridview : "The current TransactionScope is already complete"

我正在对从Gridview发送的事件进行级联删除。删除在事务中。这是简化的代码:protectedvoidbtnDeleteUser_Click(objectsender,EventArgse){DataContextdb;db=newDataContext();using(TransactionScopets=newTransactionScope()){try{//deletesomedatadb.SubmitChanges();ts.Complete();}catch(Exceptionex){//handleerror}finally{db.Dispose();BindGrid