473,394 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Getting the century only from a year

Is there a format that can be used in DateTime.ToString() that will
allow you to get only the century of a given year. "yy" of course
doesn't work, that returns the year itself, not the century.

Mar 21 '06 #1
5 18649
"Doug" <dn******@dtgnet.com> wrote:
Is there a format that can be used in DateTime.ToString()
that will allow you to get only the century of a given year.
"yy" of course doesn't work, that returns the year itself,
not the century.


Well, get the full year that way, extract the first two digits of the
year (e.g. "19" from "1984"), and add one (since 19xx is in the 20th
century, for example).

string firstTwoDigits = yearString.Substring(0, 2);
int century = Convert.ToInt32(firstTwoDigits) + 1;

You don't have to worry about years with fewer than two digits,
because years before about 1750 aren't supported. (That's when the
modern Gregorian calendar came into force.)

Eq.
Mar 21 '06 #2
It could be nice to have the century before the Gregorian calender too. :o)

using System;
using System.Collections.Generic;
using System.Text;

namespace Century
{
class Program
{

public int Cent(int year)
{
if (year.ToString().Length == 1)
return 1;
else if (year.ToString().Length == 2)
return 1;
else if (year.ToString().Length == 3)
return (Convert.ToInt32(year.ToString().Substring(0, 1))) +
1;
else if (year.ToString().Length == 4)
return (Convert.ToInt32(year.ToString().Substring(0, 2))) +
1;
else
return -1;
}

public Program()
{
System.Console.WriteLine("" + Cent(1));
System.Console.WriteLine("" + Cent(13));
System.Console.WriteLine("" + Cent(171));
System.Console.WriteLine("" + Cent(1714));
System.Console.WriteLine("" + Cent(2006));
System.Console.ReadKey();
}

static void Main(string[] args)
{
new Program();
}
}
}
Regards,
Lars-Inge Tønnessen
Mar 21 '06 #3
There's no way to use a format string of some kind? Like you can use
MM for month and yy for year and so on, there's nothing to represent
just the century?

Mar 21 '06 #4
"Doug" <dn******@dtgnet.com> wrote:
There's no way to use a format string of some kind?
Like you can use MM for month and yy for year and
so on, there's nothing to represent just the century?


That's correct. Look up "DateTimeFormatInfo" in the Visual Studio help
file, if you have it. That will show you the complete list, with no
option for centuries.

Eq.
Mar 21 '06 #5
"Paul E Collins" <fi******************@CL4.org> wrote in news:dvpspf$6mo$1
@nwrdmz03.dmz.ncs.ea.ibs-infra.bt.com:

Well, get the full year that way, extract the first two digits of the
year (e.g. "19" from "1984"), and add one (since 19xx is in the 20th
century, for example).

string firstTwoDigits = yearString.Substring(0, 2);
int century = Convert.ToInt32(firstTwoDigits) + 1;


Why not just (dtDateTimeVar.Year / 100) + 1 ?

-mdb
Mar 21 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: behindspace | last post by:
this is the error that I'm getting: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /**/**/**/content/bin/tests/price/searchHist.php on line 48 and here is...
303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
36
by: Lindie | last post by:
The more I read the more confused I get. Too much on dates calulations in the groups. I need to know how often a book has been loaned out over the past year- 52 weeks. My table has Book...
5
by: Nathan Sokalski | last post by:
I have a user control that contains three variables which are accessed through public properties. They are declared immediately below the "Web Form Designer Generated Code" section. Every time an...
2
cassbiz
by: cassbiz | last post by:
I am using strtotime and I have read up on some examples and am getting the wrong output, it jumps by several days instead of one day at a time. Ultimately what I am trying to accomplish is to set...
1
by: simbarashe | last post by:
Hie could someone please help me with getting and using the current page url. I have a function that gets the url, I want to use it with header(location : XXX) but it wont work. The code is as...
4
by: My Pet Programmer | last post by:
Ok guys, I'm really looking for someone to tell me how bad a hack this is, and if I'm close to where I should be with it. The basic situation is that I have a class which creates a basic...
7
vikas251074
by: vikas251074 | last post by:
I am getting error above in following code since few days giving tension day and night. How can I solve this? I am facing since Oct.25. in line no. 362 After doing a lot of homework, I am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.