0

I'm trying to convert this bit of Delphi code to C#:

Date1.Text:=FormatDateTime2('YYYY,JJJ/HHNNSS)

Which contains the year, the Julian day (with January 1st of each year being 1), and hours/minutes/seconds.

So an example for today would be 2016,054/090722.

The FormatDateTime2 is a very, very long function with zero comments and unhelpful variable names in the delphi code, and I'd rather not spend a long time trying to convert that over.

Normally I would have something like this:

<TextBox Name="Date1" Text="{Binding Source={x:Static System:DateTime.Now}, Mode=OneWay, StringFormat='yyyy,JJJ/hhmmss'}" />

But the JJJ day part doesn't work of course, because it isn't part of normal System.DateTime. Is there a simple way of doing this?

pfinferno
  • 1,779
  • 3
  • 34
  • 62
  • 2
    Does it make sense http://stackoverflow.com/questions/5248827/convert-datetime-to-julian-date-in-c-sharp-tooadate-safe? – Valentin Feb 23 '16 at 14:15
  • 2
    In C#, there is no way to do it in `StringFormat` as far as I know but you can calculate it with using `DayOfYear` property. – Soner Gönül Feb 23 '16 at 14:16
  • This is a dupe, but I originally linked to a question going in the other direction. Could somebody else please close to an appropriate dupe. @pfinferno, the Delphi code you posted does not compile. – David Heffernan Feb 23 '16 at 14:26
  • Sorry, didn't want to post the FormatDateTime2 function written in the Delphi code, it was several hundred lines long and very messy (I didn't write it). – pfinferno Feb 23 '16 at 14:29
  • My point was that `FormatDateTime2('YYYY,JJJ/HHNNSS)` does not compile and is in any case incomplete. – David Heffernan Feb 23 '16 at 14:37

1 Answers1

2

Can't you format it manually? DateTime has a DayOfYear property:

DateTime now = DateTime.Now;
Date1.Text = String.Format("{0},{1}/{2}", 
                 now.Year, 
                 now.DayOfYear.ToString("d3"), 
                 now.ToString("HHmmss"));
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939