如何在vb.net中找到前一天 [英] how to find previous day in vb.net

查看:174
本文介绍了如何在vb.net中找到前一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个文本框,如年月和日,并输入像28年2月作为nd 3天所以在vb.net中找到我的出生日期日期

解决方案

< blockquote>我猜你可能正在寻找找到生日的解决方案,你可以通过添加年,月和日的值(以负数形式减去它们)来实现。



我使用下面的C#代码来获取我的出生日期,它写得很完美。



 使用系统; 

public class 计划
{
public static void Main()
{
Console.WriteLine(
DateTime.Now.AddYears(-19).AddMonths(-8).AddDays( 9
.ToString( MMMM dd,yyyy on dddd
);
}
}

// 输出日期:1995年8月29日在星期二





既然你想在VB中获取它,那么代码就是......



 公开 班级计划
公开 共享 Sub Main()
Console.WriteLine(
DateTime.Now.AddYears(-19)
.AddMonths(-8).AddDays( 9
.ToString( MMMM dd,yyyy on dddd

结束 Sub
结束





请记住,您要更换t他不断将值作为变量。所以你的代码可能是这样的,



 公共 程序
公共 共享 Sub Main()
Dim year As 整数,月作为 整数,天< span class =code-keyword> As Integer
Console.WriteLine(
DateTime.Now.AddYears(year)
.AddMonths(month).AddDays(day)
.ToString( MMMM dd,yyyy on dddd

结束 Sub
结束 Class





然后根据当前日期或月份将值传递为负数或正数。它会为您提供所需的结果。


除了解决方案1之外,这里还包括输入方面:



我建议你使用DateTimePicker控件而不是三个TextBox。 DateTimePicker将使您无需解析TextBox中的输入并将值转换为DateTime(其中输入的值可能无效以表示日期)。这对用户来说也会更方便。



通常,为了从文本中解析整数,我建议使用Int32.TryParse(..)因为这样可以避免抓住潜在的例外。但是,既然你还需要从年,月和日值构造一个DateTime对象,并且没有简单的方法来做到这一点,同时避免潜在的异常,我会这样做(如果我不想因任何原因使用DateTimePicker):



  Dim  dayBefore  As  DateTime 

尝试
Dim 作为 整数 = Int32 .Parse(YearInput.Text)
Dim month As Integer = Int32 .Parse(MonthInput.Text)
Dim day 作为 整数 = Int32 .Parse(DayInput.Text)

Dim enteredDate As 日期时间(年,月,日)
Dim oneDay 作为 TimeSpan( 1 0 0 0
dayBefore = enteredDate - oneDay

Catch ex As 例外
dayBefore = DateTime。 MinValue
结束 尝试

如果 dayBefore<> DateTime.MinValue 然后
' 在这里做点什么
否则
' 向用户报告输入错误
结束 如果


I have three text box like year month and day and enter like 28 year 2 month as nd 3 days so find my date fo birth date in vb.net

解决方案

I guess you are probably looking for a solution to find the birthday, and you can do so by adding the values of years, months and days (in a negative form as to subtract them).

I used the following C# code to get my date of birth, it writes it perfectly.

using System;

public class Program
{
    public static void Main()
    {
        Console.WriteLine(
            DateTime.Now.AddYears(-19).AddMonths(-8).AddDays(9)
            .ToString("MMMM dd, yyyy on dddd")
        );
    }
}

// Output: August 29, 1995 on Tuesday



Since you want to get it in VB, so the code would be...

Public Class Program
    Public Shared Sub Main()
        Console.WriteLine(
            DateTime.Now.AddYears(-19)
            .AddMonths(-8).AddDays(9)
            .ToString("MMMM dd, yyyy on dddd")
        )
    End Sub
End Class



Remember that you are to replace the constant added values as variables. So your code might be like this,

Public Class Program
    Public Shared Sub Main()
        Dim year As Integer, month As Integer, day As Integer
        Console.WriteLine(
            DateTime.Now.AddYears(year)
            .AddMonths(month).AddDays(day)
            .ToString("MMMM dd, yyyy on dddd")
        )
    End Sub
End Class



Then pass the values as negative or positive depending on current date or month. It would give you with the required result that you want to get.


In addition to solution 1, here covering the input-aspect:

I would suggest you to use a DateTimePicker-control instead of three TextBoxes. The DateTimePicker would spare you to parse the input from the TextBoxes and to convert the values into a DateTime (where the entered values could be invalid for representing a date). It would also be more convenient for the user.

Normally, for parsing an integer from text I would suggest to use Int32.TryParse(..) because that avoids having to catch a potential exception. But since you would then also need to construct a DateTime-object from the year, month and day-values and there's no easy way to do this while avoiding a potential exception, I would do it like this (if I didn't want to use a DateTimePicker for whatever reason):

Dim dayBefore As DateTime

Try
    Dim year As Integer = Int32.Parse(YearInput.Text)
    Dim month As Integer = Int32.Parse(MonthInput.Text)
    Dim day As Integer = Int32.Parse(DayInput.Text)

    Dim enteredDate As New DateTime(year, month, day)
    Dim oneDay As New TimeSpan(1, 0, 0, 0)
    dayBefore = enteredDate - oneDay

Catch ex As Exception
    dayBefore = DateTime.MinValue
End Try

If dayBefore <> DateTime.MinValue Then
    ' do something here
Else
    ' report input error to user
End If


这篇关于如何在vb.net中找到前一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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