MM-dd-yyyy to Julian yyddd - c#

I am trying to get a label to display the Julian date in a specific format. The last two digits of the year then the day in the year, so for example January 1 2021 would be 210001. I am having difficulty getting it to display both of these values attached and making the day slot have 3 values instead of 2.
This is what I have.
This just gives the day of the year but still not as a 3 digit day so it shows 1 as 1 instead of 001 which is my goal
Any help would be appreciated! :)

Unfortunately the date time formats do not include anything for day of the year so you'll have to create this yourself. You can format a number to have leading zeros using the D format where you specify the length you want. You can however use the date formats to get the last two digits of the year. So the following should give you the desired formatted string for a date.
public string ToYYJJJ(DateTime date)
{
return date.ToString("yy") + date.DayOfYear.ToString("D3");
}

Related

How to get the number of days in a year from a given date

I need to calculate the day in a year from a given date, considering
the year too.
I'm in Brazil, so I'm using in the dd/mm/yyyy format
Console.WriteLine("Please inform the date in format dd/mm/yyyy");
DateTime dateInformed = DateTime.Parse(Console.ReadLine());
.
.
.
Console.WriteLine("The day of the year is:" + );
Examples:
If I receive: 21/10/2016 I show the number of days in 2016
If I receive: 21/10/1993 I show the number of days in 1993
Considering if it's leap year or not.
Sorry if I didn't explained well.
Thanks!
EDIT:
Some people reported as duplicate question, but I don't think so.
Let me try to explain in another way...
I'm going to receive the complete date from the user in Console.
I didn't figure out how to get the number of days in the year that the user gave to me in the format dd/mm/yyyy.
I think the calc is basically: date that the user entered - first day of the year. But I also need to know if this year is leap year or not.
But i don't know how to do this using DateTime in C#.
Another example...
25/10/2016 - 01/01/2016 = day 298 of the year
25/10/2015 - 01/01/2015 = day 297 of the year
I just have an idea receiving the day, month and year in different entrances.
Thank you!
Create a new DateTime object set at the last day of the year (31. December) and use the DayOfYear property.
int daysInYear=new DateTime(dateInformed.Year,12,31).DayOfYear;
Or check if it's a leap year using the IsLeapYear method:
int daysInYear=dateInformed.IsLeapYear()?366:365;
Reviewing the answers, my question, etc... I figure out how to know the day o the year from the given date...
That's my code in pt-br, but in the format aaaa-mm-dd.
using System;
public class Program
{
public static void Main()
{
Console.WriteLine("Informe a data no formato aaaa-mm-dd");
DateTime dataInformada = DateTime.Parse(Console.ReadLine());
Console.Write("Dia do ano: " + dataInformada.DayOfYear);
}
}
Thanks for everyone that tried to help me!

How to Cut Datetime Data Type in ASP.Net

I have a Datetime data type 12/30/2015 11:30:00 AM so, while on the SQL server, it will be fromDate.ToString ("yyyy-MM-dd HH: mm tt") results it is 2016-01-01 12: 30: 00,000 and I want to cut the string data portion only days and hours and minutes
change your
.ToString ("yyyy-MM-dd HH: mm tt")
//showing: year (yyyy), month (MM), day of month (dd), hour (HH), minute (mm), and "The AM/PM designator" (tt)
to
.ToString ("dd HH:mm")
//showing: day of month (dd), hour (HH), and minute (mm) only
More about DateTime format which you might find very useful. The explanations are given with handy samples and are quite complete.
Edit:
To interpret such text data from SQL, use .Substring(int index, int length)
string text = sqltext.Substring(("2016-01-").Length, 9); //the 9 must be from your days to your minutes. If the text string is shorter/longer, change this value
Here is the explanation of the two Substring arguments:
index argument starts from 0 offset of the text.
Suppose your text is 2016-01-01 12: 30: 00,000 then index = 0 will point to the first 2, index = 1 to the first 0 and so on. To grab the day part's first digit 0, you need to have index of 7 according to your input. But easier way to determine the index is by taking all the string before your wanted position and grab its Length (which is what I show: "2016-01-").Length.
length argument gives the number of characters you want to take from index point onward.
Since you want to take 01 12: 30 consisting total of 9 characters (note your white spaces) you should put 9. Or you can be more generic by putting ("01 12: 30").Length too:
string text = sqltext.Substring(("2016-01-").Length, ("01 12: 30").Length);
Additional note by Mr. James (see comment): "Please note that the "days" given for this answer is the day of the month, which can in no case be more than 31... If you want the number of days from some baseline date, you'd have to do DateTime subtraction and use a TimeSpan.ToString."

+/- 1 month to the datetime

What will happen when I minus 1 month to the datetime when the datetime's month falls on December? Vice versa to plus 1 month too.
Will the new datetime's year automatically change accordingly?
Thanks~
The year does automatically change with adding and subtracting months. See This Example.

Possible to convert string date to mysql DateTime?

I am working on a program that gets a list of file names from a directory, then parses the file names into individual variables (strings) and converts it into something that can be submitted to a mysql database that will the later be queued for searches.
In those file names contain a 4 digit year, 2 digit day, 2 digit month, 2 digit hour, and 4 digit minute (##AM/PM), followed by 2 numbers that can be between 3 and 11 digits.
I have parsed the filename and formatted the date and time info into the following string: YYYY/DD/MM HH:MMAM or YYYY/DD/MM HH:MMPM (only "AM" and "PM" changes on minutes).
EX: 2014/24/12 02:50PM
How can I convert the string into DateTime to submit into a MySql database.
The built-in STR_TO_DATE() function does what you need.
STR_TO_DATE('2014/24/12 02:50PM', '%Y/%d/%m %h:%i%p')
gives the result you need. You can look up the format codes here.
Using DateTime.ParseExact will do the trick to get it into .net's DateTime object:
string s = "2014/24/12 02:50PM";
DateTime dt;
DateTime.TryParseExact(s, "yyyy/dd/MM hh:mmtt", new CultureInfo("en-US"), DateTimeStyles.None, out dt) ;
Console.WriteLine(dt.ToString());

format datetime based on users culture

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.

Categories

Resources