datetime help from usa to iso

R

rodchar

hey all,

i have a datetime variable:
varDate = 4/1/2009

is there an easy way to get in following format:
varDate = 2009/04/01

the reason is because i need the 4 to have a 04 in front of it.
is there an easier way?

thanks,
rodchar
 
R

rodchar

ok, that worked,
however,
once i got it into:
varDate.ToString("yyyy/MM/dd")

i did this:
varDate.ToString("yyyy/MM/dd").SubString(5,2)
to extract the month. was that the best way?



Nicholas Paldino said:
rodchar,

You might want to use the standard format patterns "o", "s" or "u" when
calling ToString (or some other method that takes formatters).

Or, if you want the string in the format you specified, you can use the
pattern "yyyy/MM/dd" (case is important here).

See the following for more information:

http://msdn.microsoft.com/en-us/library/az4se3k1.aspx

http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

rodchar said:
hey all,

i have a datetime variable:
varDate = 4/1/2009

is there an easy way to get in following format:
varDate = 2009/04/01

the reason is because i need the 4 to have a 04 in front of it.
is there an easier way?

thanks,
rodchar
 
N

Nicholas Paldino [.NET/C# MVP]

Why use the string to extract the month? Why not just use
varDate.Month?

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

rodchar said:
ok, that worked,
however,
once i got it into:
varDate.ToString("yyyy/MM/dd")

i did this:
varDate.ToString("yyyy/MM/dd").SubString(5,2)
to extract the month. was that the best way?



Nicholas Paldino said:
rodchar,

You might want to use the standard format patterns "o", "s" or "u"
when
calling ToString (or some other method that takes formatters).

Or, if you want the string in the format you specified, you can use
the
pattern "yyyy/MM/dd" (case is important here).

See the following for more information:

http://msdn.microsoft.com/en-us/library/az4se3k1.aspx

http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

rodchar said:
hey all,

i have a datetime variable:
varDate = 4/1/2009

is there an easy way to get in following format:
varDate = 2009/04/01

the reason is because i need the 4 to have a 04 in front of it.
is there an easier way?

thanks,
rodchar
 
R

rodchar

well because it only gave me the 4 and not the 04.

Nicholas Paldino said:
Why use the string to extract the month? Why not just use
varDate.Month?

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

rodchar said:
ok, that worked,
however,
once i got it into:
varDate.ToString("yyyy/MM/dd")

i did this:
varDate.ToString("yyyy/MM/dd").SubString(5,2)
to extract the month. was that the best way?



Nicholas Paldino said:
rodchar,

You might want to use the standard format patterns "o", "s" or "u"
when
calling ToString (or some other method that takes formatters).

Or, if you want the string in the format you specified, you can use
the
pattern "yyyy/MM/dd" (case is important here).

See the following for more information:

http://msdn.microsoft.com/en-us/library/az4se3k1.aspx

http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

hey all,

i have a datetime variable:
varDate = 4/1/2009

is there an easy way to get in following format:
varDate = 2009/04/01

the reason is because i need the 4 to have a 04 in front of it.
is there an easier way?

thanks,
rodchar
 
N

Nicholas Paldino [.NET/C# MVP]

So then you convert that using a numeric formatter:

varDate.Month.ToString("00");

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

rodchar said:
well because it only gave me the 4 and not the 04.

Nicholas Paldino said:
Why use the string to extract the month? Why not just use
varDate.Month?

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

rodchar said:
ok, that worked,
however,
once i got it into:
varDate.ToString("yyyy/MM/dd")

i did this:
varDate.ToString("yyyy/MM/dd").SubString(5,2)
to extract the month. was that the best way?



:

rodchar,

You might want to use the standard format patterns "o", "s" or "u"
when
calling ToString (or some other method that takes formatters).

Or, if you want the string in the format you specified, you can
use
the
pattern "yyyy/MM/dd" (case is important here).

See the following for more information:

http://msdn.microsoft.com/en-us/library/az4se3k1.aspx

http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

hey all,

i have a datetime variable:
varDate = 4/1/2009

is there an easy way to get in following format:
varDate = 2009/04/01

the reason is because i need the 4 to have a 04 in front of it.
is there an easier way?

thanks,
rodchar
 
G

Göran Andersson

rodchar said:
ok, that worked,
however,
once i got it into:
varDate.ToString("yyyy/MM/dd")

i did this:
varDate.ToString("yyyy/MM/dd").SubString(5,2)
to extract the month. was that the best way?

No, that's not the best way. You can for example change the formatting
string to only include the month:

varDate.ToString("MM")
 
J

Joern Schou-Rode

So then you convert that using a numeric formatter:

varDate.Month.ToString("00");

.... or convert the DateTime using a month formatter:

varDate.ToString("MM")
 
A

Arne Vajhøj

rodchar said:
i have a datetime variable:
varDate = 4/1/2009

is there an easy way to get in following format:
varDate = 2009/04/01

See below.

Arne

=======================

using System;
using System.Globalization;

namespace E
{
public class DateFun
{
private const string US = "M/d/yyyy";
private const string ISO = "yyyy/MM/dd";
private static CultureInfo ci;
static DateFun()
{
ci = (CultureInfo)CultureInfo.CurrentCulture.Clone();
DateTimeFormatInfo dtf =
(DateTimeFormatInfo)ci.DateTimeFormat.Clone();
dtf.DateSeparator = "/";
ci.DateTimeFormat = dtf;

}
public static string US2ISO(string s)
{
return DateTime.ParseExact(s, US, ci).ToString(ISO, ci);
}
public static string ISO2US(string s)
{
return DateTime.ParseExact(s, ISO, ci).ToString(US, ci);
}
public static void Main(string[] args)
{
Console.WriteLine(US2ISO("4/1/2009"));
Console.WriteLine(ISO2US("2009/04/01"));
Console.ReadKey();
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top