I'm trying to make a subscription system where each user has a specific number of days on their account and everyday the number of days goes down by one until it reaches 0 days. How can I decrease the number of days of all accounts that have over 0 days, everyday and also output the date when the number of days will reach 0 in the format DD-MM-YYYY.
Thanks.
EDIT:
I found a way to convert days into date form in C# but how do I convert a date format into just the number of days?
Converting days into date format:
DateTime.Now.AddDays(subDayz).ToString("d-MMM-yyyy", new CultureInfo("en-GB"));
Maybe there is a way to do it in PHP, or do it in C# but prevent the user from changing the date to get more days?
Instead of a countdown, when you create a new account set a database column to the date when the account expires (aka when the countdown ends) in DD-MM-YYYY format. For example, if the account is created today ("31-03-2018") and expires in 15 days, set this field to "15-04-2018".
Now there's no need to update this (and all other active accounts) every day! If in the future the current day's date is greater than this expiry date, then the account is expired (aka countdown ended).
BTW, to make it easier to compare dates, store in YYYY-MM-DD format and do simple string comparison. Reformat if needed and as required for display purposes.
Related
I am storing date and time separately from Angular to C#. While storing in database, I combine start date and start time field in C# and store it in dAtabase UTC like this: "2020-02-28T22:30:30Z".
While returning from c#, i create a new DateTime with start date and start time and return as one variable. However, if the date is 28/02/2020 and time is 4.00 am, with timezoneoffset of 5.30 India, the date gets rendered to 29/02/2020 4.00 am.
Is it possible to get date and time in Angular and render it separately as string etc.
Thanks
In JavaScript Date is a timestamp, counts the number of miliseconds since January 1, 1970 00:00 UTC. So you might be having a problem with timezones. Check this answer I gave a couple of weeks ago, it may guide you. I also add a function to solve it and some references there, Subtract day in conversion between Moment and LocalDate.
angular set timezone based on user browser timezone so you can change datetimes to another timezone. please read it How to convert Date in angular to another time zone
I am setting up a system to gather data from a database based on a user inputted start date and end date. The system will gather data averaged over an interval(1 hour, 6 hours, or one day for example). If the user does not input a start or end date I would like the program to set the start date to the current time minus the interval.
I currently have the user inputting the interval in the following format.
1m = 1 minute
1h = 1 hour
12h = 12 hours
3d = 3 days
So these values are not formatted like datetime. I could take the current datetime and subtract it by either minutes, hours, or days depending on the value appended (splitting on the number), but this would mean many if statements. What I would really like is a method to subtract a datetime by an arbitrary value Does anyone have a better solution?
Instead of providing predefined time intervals (that are implemented e. g. via a separate type/enum), it is much easier to let the user freely specify a TimeSpan.
This has two advantages:
The user is not restricted to predefined intervals
You can subtract the TimeSpan directly from your DateTime.Now
If restriction to limited intervals is a requirement, you can implement this in the view/window. But still this should be a TimeSpan.
How can I format the date and time depending on the users region settings in an ASP.NET-MVC application without worrying about the order of the date?
For example, I want to have:
the day with a leading zero (dd);
month abbreviated three-letter form (MMM);
full year (yyyy);
the time just the hours and minutes both with leading zeros (HH:mm);
depending on if the user is from USA show AM/PM after the time;
Every country displays the date in a different order. USA displays first the month than days than years (MMM/dd/yyyy). In China first the year than month than day (yyyy-MMM-dd) (IIRC). And in Europe most countries display the date in this format: dd-MMM-yyyy.
And then not to mention the slashes/dashes used to separate the month, days and years from each other in every country.
This of course can be done with an endless if/else or switch statement, but isn't there a more elegant way to do this?
EDIT this is the best I came up with:
var cltr = System.Threading.Thread.CurrentThread.CurrentCulture;
DateTime.Now.ToString(cltr.DateTimeFormat.ShortDatePattern + " " + cltr.DateTimeFormat.ShortTimePattern)
It displays the date in numbers only. How would I change that in short month notation but not changing the order and the separators etc?
You can get the culture currently used by the user with CurrentCulture and CurrentCultureInfo.
For more details, see: http://msdn.microsoft.com/en-us/library/bz9tc508.aspx
I think you are talking about formatting issue. Once you have your DateTime object, then you can display it as you want. You have to specify new CultureInfo("en-US") as IFormatProvider in the DateTime.ToString() method if you want to show the datetime as US format.
I have an application which records statistics. When a user submits their statistics the information is stored and the date/time is recorded as UTC e.g.
In the UK if I recorded figures at 03/08/2010 10:30 my recorded date/time would be 03/08/2010 09:30 as we are currently observing DST.
If I was in say Australia, and I recorded my statistics at the same time locally my recorded date/time would be 02/08/2010 23:30 as I think they are around 10 hours ahead.
My application needs to generate a statistical report at 7am every morning (local time) for everyone. So when it runs it does something like this:
var currentUtc = DateTime.UtcNow.AddDays(-1); // UTC date/time 24 hours from current
This date (time is ignored) is then used to pull the statistics from the database. Now this is working fine for the UK. It is other international countries I am finding the problems. More specifically Australia.
The issue I have is Australia are 10 hours ahead so my application is being triggered to run at around 03/08/2010 22:00 UK time to generate a report for 04/08/2010 07:00 Australia time. So what is happening is something like:
// the date/time at this point would be 03/08/2010 22:00 (UTC 03/08/2010 21:00)
var currentUtc = DateTime.UtcNow.AddDays(-1);
// so currentUtc is set to 02/08/2010 22:00 (which makes sense for UK, but not for Aussie)
If the above was a report for someone in the UK it means they would receive a report for 02/08/2010 on 03/08/2010 which is correct. However, for Australia (as they are 10 hours ahead) that means they would be receiving a report for 02/08/2010 on 04/08/2010 which would be 2 days behind.
What should be happening is when my application runs at 22:00 hours on 03/08/2010 it should be generating a report for Australia which is for 03/08/2010. However, as per the setup above you can see why this isn't happening.
I can't quite seem to put my finger on this one? I have a feeling it is something to do with my ignoring the time and just relying on the UTC date but I am not sure.
Because the user's day may not start and end at midnight UTC time, your date field in the database needs to include the time as well. Otherwise your Australian report will contain more than 24 hours of statistics. The date range from 02/08/2010 to 04/08/2010 only looks odd if you ignore the time. If you include the time and double-check the range, you'll see that it does contain 24 hours just like intended. When you convert it back to local time for displaying the report it will be fine.
the mismatch between "query at 7am local time" and "previous day UTC" seems to be odd IMHO - I'd imagine the user would instead want the previous day of data, with 'day' being in their local time, so the range you would want to search within is DateTime.Today.AddDays(-1) to DateTime.Today (so it covers 'yesterday, local time'), and since you're storing in UTC, we just add in ToUniversalTime to each of those to get them into UTC for querying the database.
var startOfToday = DateTime.Today.ToUniversalTime();
var startOfYesterday = startOfToday.AddDays(-1);
var query = ...
I'm working on a small web form that requires the user to input (among other things), the scheduled backup time of whatever server they're adding to the system. The problem is, I'm struggling to find out the best way to take the user input and build a DateTime object (which is what the database requires).
I only really care about the Day of Week, Time of Day (12 or 24 hour clock).
I thought about just creating an empty DateTime object and then just adding my input values from the user, but you can only get, not set, the day of week, time of day, etc.
I've been looking at the Calender asp control, which would work for the day of the week selection, but I can't seem to find any support of time of day.
Thanks.
I don't think you want to use a DateTime for a recurring event such as a backup. A DateTime is useful for storing a particular date and time, but not a "template" for a recurring event. Instead I'd use separate columns to store the day of week value (0-6) and time of date (minutes after midnight) for the event.
If you going to use datepicker here is one great sample for adding JQuery date picker using C#. That helped me including in my project evrn if I did know anything abaut JQuery and java sripts at all.
DateTime is a immutable value type. You cannot set anything on it.
Assumed that you stick with DateTime on the DB and you don't want to use a DateTimePicker control.
You have to specify how the day of week and the time should be represented in the DateTime. You can start with DateTime.MinValue, the 1.1.0001, 12:00 at midnight, and add the day of week and the time. unfortunately, a regular DateTime field in a SqlServer 2005 is not able to store this date. So lets move it to the year 2000. The 1.1.2000 was a Saturday. You could calculate the DateTime like this:
int dayOfWeek; // 0 = mon, 6 = son
DateTime time;
DateTime scheduleTime = new DateTime(2000, 1, (dayOfWeek + 2) % 6 + 1)
+ time.TimeOfDay;
But honestly, I wouldn't do it. It smells. I just answered your question. Listen to tvanfosson. He said everything that needs to be said.