在服务中生成随机日期和时间 [英] Generate random date and time in a service

查看:103
本文介绍了在服务中生成随机日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用一种可以由表单访问的服务中的方法,以便我可以使用此方法在日期时间选择器中生成随机日期。它只是不起作用,我有两个DTP称为dtp_Current和dtp_New



这是在表格上访问的,我有两个按钮,说明之前和之后,当在单击之前,您猜测新生成的日期将在当前日期之前,如果您在猜测之后单击它将在当前生成的日期之后。我必须使用服务这样做



public int RandomDate()



是我的意思d喜欢在服务中调用的方法,如何执行此操作,以便在单击after按钮时检查dtp_Current日期以查看dtp_New是否更大



我希望这很有意义



摘要:

有表格和服务参考

需要服务参考在dtp_Current生成随机日期

然后在点击之前或之后在dtp_New中生成一个新日期

然后检查dtp_New是否大于或小于dtp_Current







SAVE ME ORIGINALGRIFF

I'm trying to have a kind of method that is in a service that can be accessed by a form so that I can use this method to generate a random date in a date time picker. It just doesn't work however, I have two DTP's called dtp_Current and dtp_New

This is accessed on a form and I have two buttons that say before and after, when before is clicked you are guessing the newly generated date will be before the current date and if you click after it's guessing it's gonna be after the currently generated date. I have to do this using a service however

public int RandomDate()

is what i'd like the method to be called in the Service, how would I go about doing this so when the after button is clicked it checks dtp_Current date to see if dtp_New is larger

I hope this makes sense

summary:
have a form and a service reference
need service reference to generate a random date in dtp_Current
then when before or after is clicked generate a new date in dtp_New
then to check if dtp_New is larger or smaller than dtp_Current



SAVE ME ORIGINALGRIFF

推荐答案

漂亮很简单 - DateTime有一个带Ticks计数的构造函数: http:// msdn.microsoft.com/en-us/library/z2xf7zzk(v=vs.110).aspx [ ^ ]所以你需要做的就是生成一个随机的Int64:

Pretty easy really - DateTime has a constructor that takes a Ticks count: http://msdn.microsoft.com/en-us/library/z2xf7zzk(v=vs.110).aspx[^] so all you need to do is generate a random Int64:
public static Int64 NextInt64(this Random rand)
    {
    var buffer = new byte[sizeof(Int64)];
    rand.NextBytes(buffer);
    return BitConverter.ToInt64(buffer, 0);
    }

您可能需要调整一下才能获得合适的值 - 但这仍然是您的日期范围!

You might have to fiddle a bit to get "suitable" values - but that's down to your date range anyway!


如果您考虑到日期时间结构最终代表了许多抽搐,你可以生成一个随机值并用它来初始化那个抽搐数的日期时间。



DateTime(Int64)



我作为练习留给你如何使用适当的随机函数创造价值。



If you consider that the datetime structure ultimately represents a number of tics, you can generate a random value and use it to initialize a date-time to that number of tics.

DateTime(Int64)

I leave as an exercise for you how to use the appropriate random function to create the value.


这篇关于在服务中生成随机日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆