DateTime ExpMonth = Convert.ToInt32(ddExpMonth); ---- DropDown(user selects a month)
DateTime ExpYear = Convert.ToInt32(ddExpYear); ---- Dropdown(user selects year)
Datetime ExpDate = ///// I want this part to be saved as Datetime 02/2012
How is this possible. Or any other way.
A DateTime value doesn't know about a format - it's just a date and a time. You can create a new DateTime value with the relevant information:
DateTime expiry = new DateTime(Convert.ToInt32(ddExpYear),
Convert.ToInt32(ddExpMonth),
1);
... but how that is "saved" is entirely up to you. If you give us more information, we may be able to help you more. You can format it to a string easily enough:
string formatted = expiry.ToString("yyyy/MM");
... but that may not be what you're after.
You could store it in a DateTime as follows:
DateTime expDate = new DateTime(ExpYear, ExpMonth, 1).AddMonths(1).AddDays(-1);
If it's for a credit card expiration date, make sure the day is the last day of the month or don't compare the day. Their may be some discrepancies on the last day being expired or not. It should be still valid so make sure the current date is at least a day greater.
You will need to save this value either as a nvarchar, where you'll be able to do whatever you want, or a datetime. The difference is that the datetime format requires you to provide the day, and the time be set to midnight. The value 1, for the first day of the month should be considered here.
DateTime Today = new DateTime( DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(1).AddDays(-1);
DateTime cc = new DateTime(2016, 9, 1).AddMonths(1).AddDays(-1);
Console.WriteLine(Today.ToString());
Console.WriteLine(cc.ToString());
if (Today <= cc)
{
Console.WriteLine("Ok");
}
else
{
Console.WriteLine("Card Expiry Date is not valid ");
}
Related
I have searched online and i only managed to find codes to set the year month and day.
dateTimePicker2.Value = new DateTime(2017,12,31);
I tried using the custom format and it does not seem to work
dateTimePicker2.CustomFormat = "DD/MM";
dateTimePicker2.Value = new DateTime(12,31);
You cannot create DateTime object only from day and month. DateTime simply doesn't have this kind of constructor. DateTime Constructors
So you need to go with some kind of "workaround"
- Use "dummy" year and when you need to use a date - use only Month and Day properties.
var dummyYear = 2000;
dateTimePicker2.Value = new DateTime(dummyYear, 12, 31);
Another workaround will be to use ParseExact method which will create DateTime based on the format you are using "dd/MM"
var date = DateTime.ParseExact("31/12", "dd/MM", CultureInfo.InvariantCulture);
dateTimePicker2.Value = date; // 12/31/2017
Notice that when you did not provide a year - current year will be used.
Another notice: DD is invalid format for days it should be lower case "dd"
You cannot partially set the date without a year, it's not valid.
What you cand do is specify the month and date in code as "default" values, and get the current year programmtically (or whatever year you want), and use that value for the year.
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "dd/MM";
dateTimePicker1.Value = DateTime.Now;
I have datetime string
dateStr = "2017-03-21T23:00:00.000Z";
then I am calling
var date = DateTime.Parse(dateStr);
and unexpectedly my date equals
22.03.2017 00:00:00
I expected it to be 21.03.2017
What's going on here?
DateTime.Parse() is locale specific and will take into account your local time zone when parsing dates.
If you are in CET (Central European Time) during the winter your offset is one hour ahead of UTC. The date given is marked with a Z indicating it is in UTC, so DateTime.Parse() will adjust that to your local timezone.
There is an override that allows you to change that behaviour if you want, by specifying a specific DateTimeStyles enum. DateTimeStyles.AdjustToUniversal is what you are looking for as that should keep the DateTime as UTC.
And if you only want the date part afterwards, you can just call .Date on the DateTime object you got back from Parse()
So, something like this:
var date = DateTime.Parse(dateStr, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal).Date;
if the date format does not change then you can use the below code to get date part from date string. But it is a bit risky due to its strict dependency on the input format.
string dateStr = "2017-03-21T23:00:00.000Z";
int year = Int32.Parse(dateStr.Substring(0, 4));
int month = Int32.Parse(dateStr.Substring(5, 2));
int day = Int32.Parse(dateStr.Substring(8, 2));
var date = new DateTime(year, month, day);
Console.WriteLine(date);
Because the format of type 'DateTime' variable is 'YYYY-MM-DD hh:mm:ss'.
If you run this code:
var dt = DateTime.Now;
Console.WriteLine(dt);
You'll see '24/03/2017 12:54:47'
If you have 'YYYY-MM-DD' format, add .ToString("dd-MM-yyyy"), then:
string dateStr = "2017-03-21T23:00:00.000Z";
var date = DateTime.Parse(dateStr).ToString("dd-MM-yyyy");
Result:'24-03-2017'
I want convert long dateTime to only date in C#. and both in dateTime type
DateTime creationDate = DateTime.Now;
DateTime shortDate = creeationDate.Date;
A DateTime is a struct, which always has -- as the name suggests -- date and time components. You cannot remove the time and there is, as far as I know, no built-in date-only type in .NET (maybe there is in some 3rd party libraries).
You have already discovered the Date property of DateTime which returns an new DateTime with the time components set to 00:00:00.000.
If you only need to output your current date you must apply some formatting on the DateTime
DateTime creationDate = DateTime.Now;
string shortDate = creatationDate.ToString("yyyy-MM-dd"); //or whatever formatting you need.
If you need calculations on your date, but want to ignore the time component, you could initialize your date accordingly, as you have done in your code.
DateTime creationDate = DateTime.Now.Date; //sets time component to 00:00
DateTime otherDate = new DateTime(2016, 11, 2, 0,0,0); //also initializies with time 00:00
int diff = (int)(otherDate-creationDate).TotalDays; //number of days between the two dates
I am trying to display the time remaining until a time specified by the user. I want to show Hours, Minutes, Seconds, and maybe milliseconds until the specified time.
DateTime remaining = DateTime.Parse("2/24/2014 18:00:00 pm");
DateTime startDate = DateTime.Now;
TimeSpan t = remaining - startDate;
string countdown = string.Format("{0}:{1}:{2}:{3}", t.Days, t.Hours, t.Minutes, t.Seconds);
CountDown.Content = countdown;
Visual Studio says I need to parse the string to take in the date before setting it to a DateTime object.
So do I need to create a new string, then parse it to a string, and then set the string to a DateTime object?
Update:
The actual error message I am receiving is:
System.FormatException was unhandled HResult=-2146233033
Message=String was not recognized as a valid DateTime. Source=mscorlib
If I understand the question correctly, you just want:
DateTime target = new DateTime(2014, 2, 24, 18, 0, 0);
TimeSpan remaining = target - DateTime.Now;
There's no need to parse a string just to get a DateTime value, if you already know the year/month/day etc you want.
However, you've also talked about "a time specified by the user". If that date/time is being specified as a string, then yes, you'll need to parse it. Ideally, it would be specified by some sort of date/time picker control, in which case you should just be able to get an appropriate DateTime value. Avoid string conversions unless you really need them.
This should get you going:
DateTime remaining = DateTime.Parse("2/24/2014 18:00:00 pm");
DateTime startDate = DateTime.Now;
TimeSpan t = remaining - startDate;
DateTime difference = new DateTime(t.Ticks);
CountDown.Content = difference;
I use this to convert DateTime value into Date and then I add 00:00:00 and 23:59:59 to make sure whole day is taken into consideration when counting stuff. I'm pretty sure it's wrong way of doing things. What would be the right way?
DateTime varObliczOd = DateTime.Parse(dateTimeWycenaPortfelaObliczDataOd.Value.ToShortDateString() + " 00:00:00");
DateTime varObliczDo = DateTime.Parse(dateTimeWycenaPortfelaObliczDataDo.Value.ToShortDateString() + " 23:59:59");
if dateTimeWycenaPortfelaObliczDataOd is of type DateTime, You can use:
dateTimeWycenaPortfelaObliczDataOd.Date
to get the date part only (time will be 00:00:00...).
If you want to get the very last tick of the date, you can use:
dateTimeWycenaPortfelaObliczDataOd.Date.AddDays(1).AddTicks(-1)
but you really better work with the next date (.AddDays(1)).
In any case, there is no need to convert to string and back to DateTime.
DateTime objects have a Date property which might be what you need.
You can use the following properties / methods on a DateTime object to get your values :
DateTime varObliczOd = dateTimeWycenaPortfelaObliczDataOd.Date;
DateTime varObliczDo = dateTimeWycenaPortfelaObliczDataOd.AddDayes(1).AddTicks(-1);
It would help to know why you're needing it, but this would work.
DateTime varObliczOd = dateTimeWycenaPortfelaObliczDataOd.Date;
DateTime varObliczDo = varObliczOd.AddDays(1).AddSeconds(-1);
Using the Date attribute and then manipulating them directly to create the required time component - no need to bother with parsing and conversion.
You could use the Date property of the DateTime object to accomplish what you need.
DateTime varObliczOd = dateTimeWycenaPortfelaObliczDataOd.Value.Date;
DateTime varObliczDo = dateTimeWycenaPortfelaObliczDataDo.Value.Date.AddDays(1);
If you really want it to end at 23:59:59 you can do:
DateTime varObliczDo = dateTimeWycenaPortfelaObliczDataDo.Value.Date.AddDays(1).AddSeconds(-1);
Will set varObliczDo to be your ending date with no time plus one day (at midnight). So if dateTimeWycenaPortfelaObliczDataDo was 2010-03-05 16:12:12 it would now be 2010-03-06 00:00:00.
Something like this maybe? I've typed this out of my head, there are probably some mistakes in the code.
DateTime varObliczOd = dateTimeWycenaPortfelaObliczDataOd.AddSeconds(-dateTimeWycenaPortfelaObliczDataOd.Seconds).AddMinutes(-dateTimeWycenaPortfelaObliczDataOd.Minutes).AddHours(-dateTimeWycenaPortfelaObliczDataOd.Hours);
DateTime varObliczDo = new DateTime(dateTimeWycenaPortfelaObliczDataDo.Year, dateTimeWycenaPortfelaObliczDataDo.Month, dateTimeWycenaPortfelaObliczDataDoDay, 23, 59, 59);
DateTime newDate = new DateTime( oldDate.Year, oldDate.Month, oldDate.Day, 23, 59,59 )
DateTime newDate = new DateTime( oldDate.Year, oldDate.Month, oldDate.Day, 0, 0, 0 )
You could work with TimeSpan:
DateTime varObliczOd = dateTimeWycenaPortfelaObliczDataOd - new TimeSpan(dateTimeWycenaPortfelaObliczDataOd.Hours, dateTimeWycenaPortfelaObliczDataOd.Minutes, dateTimeWycenaPortfelaObliczDataOd.Seconds);
Like that you avoid at least the parsing, which can fail depending on the local culture settings.