Is there a way to convert a yyyy/ddd string to Date in C#?
For example 2019003 is January 3, 2019.
Also to validate if the string is yyyy/ddd format.
Kindly help
You could make a TryParse method like this:
public static bool TryParseSpecialDate(string dateString, out DateTime parsedDate)
{
parsedDate = DateTime.MinValue;
// parse yyyy/DDD into 2 separate capture groups
var match = Regex.Match(dateString ?? string.Empty, #"^(\d{4})/(\d{3})$");
if (!match.Success)
{
return false;
}
// Create a date for yyyy/01/01
var yearDate = new DateTime(int.Parse(match.Groups[1].Value), 1, 1);
var dayOfYear = int.Parse(match.Groups[2].Value);
if (dayOfYear < 1 || dayOfYear > 366)
{
return false;
}
// Add the required number of days
var result = yearDate.AddDays(dayOfYear - 1);
// Check that it's the same year (so that 2019/888 won't work, or 366 in a non leap year)
if (result.Year != yearDate.Year)
{
return false;
}
// Set the date and return it
parsedDate = result;
return true;
}
I've used regex (I didn't need to but it seemed easier, feel free to replace it with string operations instead). This then starts from January in the desired year, and adds the number of days to it.
Usage:
bool success = DateTimeHelpers.TryParseSpecialDate("2019/354", out tmp);
Output will be 2019/12/20
Try it online
You can probably do this:
string julianDate = "2019003";
int year = Convert.ToInt32(julianDate.Substring(0, 4));
int dayOfYear = Convert.ToInt32(julianDate.Substring(4));
DateTime dateTime = new DateTime(year-1, 12, 18, new JulianCalendar());
dateTime = dateTime.AddDays(dayOfYear);
This should return the desired date.
Reference:
I want to covert julian date(YYJJJ format) to any normal date format(MMDDYY) using c#. Is there any defined function for that?
Here's method to achieve that:
public DateTime ParseDateString(string strDt)
{
// validate string, pattern explanation:
// \d - match sigle digit
// ^ - match beginning of a string
// $ - match end of a string
// /? - match zero or one /
if(! Regex.Match(strDt, #"^\d\d\d\d/?\d\d\d$").Success)
throw new ArgumentException("Invalid string");
// get rid of a optional /
strDt = strDt.Replace("/", "");
var days = int.Parse(dt.Substring(4));
var year = int.Parse(dt.Substring(0, 4));
var date = new DateTime(year, 1, 1);
return date.AddDays(days - 1);
}
Related
How to get a Friday date from the given start date and end date,
For Example:
25/03/2021 - starting date
14/08/2021 - endind date
I have a class
public static class DateUtils
{
public static List<DateTime> GetWeekdayInRange(this DateTime from, DateTime to, DayOfWeek day)
{
const int daysInWeek = 7;
var result = new List<DateTime>();
var daysToAdd = ((int)day - (int)from.DayOfWeek + daysInWeek) % daysInWeek;
do
{
from = from.AddDays(daysToAdd);
result.Add(from);
daysToAdd = daysInWeek;
}
while (from < to);
return result;
}
}
That is how i call it in main method:
var from = DateTime.Today; // 25/8/2019
var to = DateTime.Today.AddDays(23); // 23/9/2019
var allFriday = from.GetWeekdayInRange(to, DayOfWeek.Friday);
Console.WriteLine(allFriday);
Console.ReadKey();
Error i get:
System.Collections.Generic.List`1[System.DateTime]
I am new and still learning, how do I call in the main method so that my output be like all dates(fridays) between the range?
Link I followed
To Answer your question, instead of printing allFridays in one go, iterate over each element of list i.e allFridays, convert into string and then print
foreach(var friday in allFridays)
Console.WriteLine(friday);
Why you are getting System.Collections.Generic.List[System.DateTime] ?
Console.WriteLine(), for non primitive type by default calls
.ToString() function which prints type of it(if it is not overridden). In your case, you
need an individual date not a type of List, so you need to iterate
each DateTime from the list and print each date.
One Liner solution:
Console.WriteLine(string.Join(Environment.NewLine, allFridays));
Alternate solution:
public static List<DateTime> GetWeekdayInRange(this DateTime #from, DateTime to, DayOfWeek day)
{
//Create list of DateTime to store range of dates
var dates = new List<DateTime>();
//Iterate over each DateTime and store it in dates list
for (var dt = #from; dt <= to; dt = dt.AddDays(1))
dates.Add(dt);
//Filter date based on DayOfWeek
var filteredDates = dates.Where(x => x.DayOfWeek == day).ToList();
return filteredDates;
}
...
var #from = DateTime.Today; // 25/8/2019
var to = DateTime.Today.AddDays(23); // 23/9/2019
var allFriday = #from.GetWeekdayInRange(to, DayOfWeek.Friday);
Console.WriteLine(string.Join(Environment.NewLine, allFridays));
.NET FIDDLE
Since in your Usage section, you have successfully get the result via GetWeekdayInRange. You can print the dates with these methods:
Method 1:
allFriday.ForEach(x => Console.WriteLine(x.ToShortDateString()));
Method 2:
foreach (var friday in allFriday)
{
Console.WriteLine(friday.ToShortDateString());
}
Method 3:
for (var i = 0; i < allFriday.Count(); i++)
{
Console.WriteLine(allFriday[i].ToShortDateString());
}
Note: ToShortDateString() is one of the methods to display Date string. You can define your desired Date pattern with ToString().
Trying to figure out how to add a century prefix (19 or 20) to a birth date. Does anyone see how to write this in a better way?
public string GetCenturyPrefix(string socSecNo)
{
string prefix = string.Empty;
try
{
var currentDate = DateTime.Now;
var birthDayTemp = socSecNo.Substring(0, 6);
var yy = birthDayTemp.Substring(0, 2);
var mm = birthDayTemp.Substring(2, 2);
var dd = birthDayTemp.Substring(4, 2);
birthDayTemp = yy + "-" + mm + "-" + dd;
var birthDay = Convert.ToDateTime(birthDayTemp);
var totalDays = currentDate - birthDay;
var age = totalDays.TotalDays / 365;
var yearsAfter2000 = Convert.ToInt32(currentDate.Year.ToString().Substring(2, 2));
if (age > yearsAfter2000)
{
prefix = "19";
}
else
{
prefix = "20";
}
}
catch (Exception)
{
}
return prefix;
}
Don't use Substring to parse a string value into a DateTime. .Net has very robust methods created for you to do this conversion.
Here I'm using DateTime.TryParseExact(), which lets me specify the exact format I expect dates values to be provided in. The method returns true or false indicating if the value is in that supplied format. No need to use exceptions to control logic flow.
public string GetCenturyPrefix(string socSecNo)
{
// Check if you're able to parse the incoming value
// in the format "yyMMdd".
if (!DateTime.TryParseExact(socSecNo, "yyMMdd", CultureInfo.InvariantCulture,
DateTimeStyles.None, out DateTime parsedDateTime))
{
// Do something if the input can't be parsed in that format.
// In this example I'm throwing an exception, but you can also
// return an empty string.
throw new Exception("Not valid date format");
}
// Extract only the Year portion as a 4 digit string,
// and return the first 2 characters.
return parsedDateTime.ToString("yyyy").Substring(0, 2);
}
You can do it like (year/100)+1 but put {0:n0} format to your .ToString("{0:n0}") this could be the logic and for the if else it can stay like that. This should be working for any century as I tried on calculator.
Below is my code. I am only getting the difference between two dates, but I want the name of that month which comes between the from and to dates.
public static int GetMonthsBetween(DateTime from, DateTime to)
{
if (from > to) return GetMonthsBetween(to, from);
var monthDiff = Math.Abs((to.Year * 12 + (to.Month - 1)) - (from.Year * 12 + (from.Month - 1)));
if (from.AddMonths(monthDiff) > to || to.Day < from.Day)
{
return monthDiff - 1;
}
else
{
return monthDiff;
}
}
Based on your code you could substract the month difference from the "to" DateTime to get DateTime difference from your input.
public static List<DateTime> GetMonthsBetween(DateTime from, DateTime to)
{
if (from > to) return GetMonthsBetween(to, from);
var monthDiff = Math.Abs((to.Year * 12 + (to.Month - 1)) - (from.Year * 12 + (from.Month - 1)));
if (from.AddMonths(monthDiff) > to || to.Day < from.Day)
{
monthDiff -= 1;
}
List<DateTime> results = new List<DateTime>();
for (int i = monthDiff; i >= 1; i--)
{
results.Add(to.AddMonths(-i));
}
return results;
}
To get the name of the month just format the DateTime to "MMM".
var dts = GetMonthsBetween(DateTime.Today, DateTime.Today.AddMonths(5));
foreach (var dateTime in dts)
{
Console.WriteLine(dateTime.ToString("MMM"));
}
If you want the names of all months between two dates, use something like this:
var d1 = new DateTime(2015,6,1);
var d2 = new DateTime(2015,9,1);
var monthlist = new List<string>();
string format = d1.Year == d2.Year ? "MMMM" : "MMMM yyyy";
for (var d = d1; d <= d2; d = d.AddMonths(1))
{
monthlist.Add(d.ToString(format));
}
The full list is now in monthlist - you will want to return that from your method.
Assuming you're using Java and JodaTime there are several flaws in your code.
You cant use from > to to evaluate if a date is after an other. Use from.isAfter(to) instead.
JodaTime already supplies a method to calculate the amount of whole months between two given Dates Months.monthsBetween(start,end).
With the calculated month difference you can instantiate a new DateTime object that holds a date in your desired month and output its name via yourNewDateTimeObject.month().getAsText().
edit: Just found out you're using C# so ignore my text above this. Below here I will try to answer your question in C#.
Why dont you just subtract the from from the to date and obtain your difference?
The resulting TimeSpan can be used to determine the amount of whole months between your two given dates.
To obtain the resulting month name you could use yourDateTime.ToString("MMMM");
I try to popup a msgbox that shows the months and years of the given dates for example
my input is:
7/2012 and 2/2013
and the output should be:
7/2012,8/2012,9/2012,10/2012,11/2012,12/2012,1/2013,2/2013
I wrote:
string datePart1;
string datePart2;
string[] date1 = new string[] { "" };
string[] date2 = new string[] { "" };
private void button1_Click(object sender, EventArgs e)
{
DateTime endDate = new DateTime(2013, 2, 1); // i will be having the date time as a variable from a textbox
DateTime begDate = new DateTime(2012, 7, 1); // i will be having the date time as a variable from a text box
int year, month;
if (endDate.Month - begDate.Month < 0)
{
month = (endDate.Month - begDate.Month) + 12;
endDate = new DateTime(endDate.Year - 1, endDate.Month, endDate.Day);
}
else
month = endDate.Month - begDate.Month;
year = endDate.Year - begDate.Year;
The above code calculates the time difference, but my attempts at outputting haven't worked.
Here's a sample to get you started.
It provides a handy MonthsInRange() method which returns a sequence of all the months in the specified range. You can then format the returned dates using "M\\/yyyy" (see below) to output the required format. (Note: That's not a letter V, it's a backslash followed by a forward slash!)
See Custom Date and Time Format Strings for an explanation of the format string.
using System;
using System.Collections.Generic;
namespace Demo
{
public static class Program
{
static void Main(string[] args)
{
DateTime endDate = new DateTime(2013, 2, 1);
DateTime begDate = new DateTime(2012, 7, 1);
foreach (DateTime date in MonthsInRange(begDate, endDate))
{
Console.WriteLine(date.ToString("M\\/yyyy"));
}
}
public static IEnumerable<DateTime> MonthsInRange(DateTime start, DateTime end)
{
for (DateTime date = start; date <= end; date = date.AddMonths(1))
{
yield return date;
}
}
}
}
Why "M\\/yyyy" and not just "M/yyyy"?
This is because the "/" character in a DateTime format string will be interpreted as the "date separator", not a literal "/". In some locales, this will come out as "." and not "/".
To fix this, we need to escape it with a "\" character. However, we can't just use a single "\" because C# itself will interpret that as an escape character, and will use it to escape the following character. The C# escape sequence for a literal "\" is "\\", which is why we have to put "\\/" and not just "\/".
Alternatively you can turn of escaping of "\" characters by prefixing the string with an # character, like so:
#"M/yyyy"
You can use whichever you prefer.
Since you're not guaranteed to have dates with the same day, you can use this code which creates new dates that only consider the first of the month.
static IEnumerable<string> InclusiveMonths(DateTime start, DateTime end)
{
// copies to ensure the same day.
var startMonth = new DateTime(start.Year, start.Month, 1);
var endMonth = new DateTime(end.Year, end.Month, 1);
for (var current = startMonth; current <= endMonth; current = current.AddMonths(1))
yield return current.ToString("M/yyyy");
}
// usage
foreach (var mmyyyy in InclusiveMonths(begDate, endDate))
{
Console.WriteLine(mmyyyy);
}
var allMonths = string.Join(", ", InclusiveMonths(begDate, endDate));
Look into using the TimeSpan structure, it'll help you achieve your goal a lot faster.
http://msdn.microsoft.com/en-us/library/system.timespan.aspx
You may use
TimeSpan dateDifference = endDate - begDate;
year = dateDifference.Days / 365;
month = dateDifference.Days / 30;
Edit:
I forgot TimeSpan does not feature Year or Month, sorry :(
Assuming you have a Unix timestamp, what would be an easy and/or elegant way to check if that timestamp was some time yesterday?
I am mostly looking for solutions in Javascript, PHP or C#, but pseudo code and language agnostic solutions (if any) are welcome as well.
In C# you could use this:
bool isYesterday = DateTime.Today - time.Date == TimeSpan.FromDays(1);
You can use this in C#:
bool isYesterday = (dateToCheck.Date.AddDays(1) == DateTime.Now.Date);
PHP:
$isYesterday = date('Ymd', $timestamp) == date('Ymd', strtotime('yesterday'));
In pseudo code, to compare timestamps:
get current Unix timestamp
transform the retrieved timestamp to a date
subtract 1 day from the date
transform the timestamp to test to a date
compare both dates. If they're equal the tested timestamp was yesterday.
Watch out for timezones if you show the results to a user. For me it's now 13:39 on July 9 2010. A timestamp for 14 hours ago for me is yesterday. But for someone in a different timezone where it's now 15:39, 14 hours ago wasn't yesterday!
Another problem might be systems with a wrong time/date setup. For example if you use JavaScript and the system time of the visitors PC is wrong, the program may come to a wrong conclusion. If it's essential to get a correct answer, retrieve the current time from a known source with a correct time.
An example in Smalltalk using Pharo/Squeak
(Date year: 2014 month: 4 day: 24) = Date yesterday
This accepts an optional DateTimeZone object. If it's not given, it uses the currently set default timezone.
<?php
function isYesterday($timestamp, $timezone = null) {
$t = new DateTime(null, $timezone);
$t->setTimestamp($timestamp);
$t->setTime(0,0);
$yesterday = new DateTime("now", $timezone);
$yesterday->setTime(0,0);
$yesterday = $yesterday->sub(new DateInterval('P1D'));
return $t == $yesterday;
}
Another C# example:
bool isYesterday = DateTime.Now.Date.AddDays(-1) == dateToCheck.Date;
Code:
static class ExtensionMethods
{
private static readonly DateTime UnixStart = new DateTime(1970, 1, 1, 0, 0, 0, 0);;
public static bool IsYesterday(this int unixTime)
{
DateTime convertedTime = UnixStart.AddSeconds(unixTime);
return convertedTime.Date == DateTime.Now.AddDays(-1).Date;
}
public static bool IsYesterday(this DateTime date)
{
return date.Date == DateTime.Now.AddDays(-1).Date;
}
}
Examples:
public class Examples
{
public void Tests()
{
if (1278677571.IsYesterday()) System.Console.WriteLine("Is yesterday");
DateTime aDate = new DateTime(2010, 12, 31);
if (aDate.IsYesterday()) System.Console.WriteLine("Is yesterday");
}
}
In JavaScript, you could write
var someDate = new Date(2010, 6, 9);
Date.yesterday.date == someDate.date // true
Left out needless implementation details, but it's possible. Ok, there ya go :)
(function() {
function date(d) {
var year = d.getFullYear();
var month = d.getMonth();
var day = d.getDate();
return new Date(year, month, day);
}
Object.defineProperty(Date, 'yesterday', {
enumerable: true,
configurable: false,
get: function() {
var today = new Date();
var millisecondsInADay = 86400000;
var yesterday = new Date(today - millisecondsInADay);
return yesterday;
},
set: undefined
});
Object.defineProperty(Date.prototype, 'date', {
enumerable: true,
configurable: true,
get: function() {
return date(this).valueOf();
},
set: undefined
});
})();
C#
TimeSpan difference = DateTime.Now.Date - olderDate.Date;
bool isYesterday = difference.TotalDays == 1;
You can give this function a shot:
public bool IsFromYesterday(long unixTime) {
DateTime convertedTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
convertedTime.AddSeconds(unixTime);
DateTime rightNow = DateTime.Now;
DateTime startOfToday = DateTime.Today;
DateTime startOfYesterday = startOfToday - new TimeSpan(1, 0, 0, 0);
if (convertedTime > startOfYesterday && convertedTime < rightNow)
return true;
else
return false;
}