C#: conversion of field to month and year format - c#-4.0

I have the line of code below
txtCustomers.Text = _myCustList[0].F5Customer.dateActive;
_myCustList[0].F5Customer.dateActive returns : 2/27/2014 12:00:00AM
I want the date format to be in [month , year]
I tried the code below but no luck
txtCustomers.Text = _myCustList[0].F5Customer.dateActive.ToString("MMMM , YYYY");
How can I get it in the [month, year] format.

What is the "month and year" format exactly? Maybe you want the complete month-name and year, then use lowercase yyyy, so "MMMM , yyyy" instead of "MMMM , YYYY":
txtCustomers.Text = _myCustList[0].F5Customer.dateActive.ToString("MMMM , yyyy");
Edit: as commented that property is a string not a DateTime. Why is a property that is called dateActive a string in the first place? I would make it a DateTime property. Otherwise you always have to parse it to a DateTime.
txtCustomers.Text = DateTime.Parse(_myCustList[0].F5Customer.dateActive).ToString("MMMM , yyyy");

Related

SAS not recognizing date format

I have the following character date format:
"3/1990"
"4/1990"
"5/1990"
...
I tried the following code:
data work.temps;
set indata;
newdate = input(strip(Date), MMYYSw.);
rename newdate = date;
run;
I keep on getting the following error meassage: Informat MMYYSW was not found or could not be loaded.
You may have to use a different informat to read in the character dates so that SAS can interpret them as numeric (since dates in SAS are actually numeric values), and then format them as MMYYS..
This was tested and works for me:
DATA temps;
FORMAT newdate MMYYS.;
SET indata;
newdate = INPUT(COMPRESS('01/'||date),DDMMYY10.);
RUN;
Try this with anydtdte:
data have;
input date $10.;
_date=input(compress(date,'""'),anydtdte.);
format _date MMYYs7.;
cards;
"3/1990"
"4/1990"
"5/1990"
;
run;
What you refer is a FORMAT, not INformat.
You'll use format with PUT function, for INPUT, you need informat.
Anyway I didn't find a suitable informat to be used directly, so you'll need to do more stuff:
data work.temps;
infile cards truncover;
input Date $10.;
newdate=MDY( scan(Date,1, '/'), 1, scan(Date,2, '/') );
cards;
3/1990
4/1990
5/1990
;
run;
SCAN takes Nth word from a string, MDY creates DATE from Month, Day and Year.
The code above gives the first day of the month.

How to get today's date in Groovy

I am trying to get today's date in format of yyyyMMdd in groovy language.
I have tried to do this:
String oldDate = '20150702'
Date date = Date.parse( 'yyyyMMdd', oldDate )
String currentDate = date.format( 'yyyyMMdd' )
Output: 20150702
I am trying to get today's date however.
Then use:
new Date().format( 'yyyyMMdd' )
The answer of #Opal did not work for me:
groovy.lang.MissingMethodException: No signature of method: java.util.Date.format() is applicable for argument types: (String) values: [yyyy-MM-dd]
Here is a solution that worked for me:
new java.text.SimpleDateFormat("yyyy-MM-dd").format(new Date())
(edit: removed extra parenth at the end of snippet)

Datetime parse exception in C#

I have tried all the ways, however not sure why the string date is not getting converted to datetime.
string windowsTime = "2/21/2009 10:35:14 PM"
DateTime time = DateTime.ParseExact(windowsTime, "MM/dd/yyyy hh:mm:ss tt", null);
I used, DateTime.Parse, ParseExact, Convert.ToDatetime.
But nothing is working, I am getting "String was not recognized as a valid DateTime.".
can somebody advise what am I doing wrong ?
Since the month has only one digit this is correct M.
You also have to use CultureInfo.InvariantCulture instead of null(means current-culture). Otherwise all / will be replaced with the actual date separator of your current culture( for me de-DE it's .):
DateTime time = DateTime.ParseExact(windowsTime, "M/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
Custom date and time format strings: the "/" Custom Format Specifier
as specified in the MSDN
"MM" The month, from 01 through 12. More information: The "MM" Custom
Format Specifier.
Custom Datetime

Convert string to Datetime Exception for 15-07-2013

I am trying to convert string to datetime. But it is giving me exceptioncontibously.
Kindly help.
DateTime dt = Convert.ToDateTime("15-07-2013");
I am getting exception as "String was not recognized as a valid DateTime."
Now i have the string as "15-07-2013 07:12:00 PM"
When i am using the code as mentioned below i am getting exception.
DateTime dtCurrentFile = DateTime.ParseExact("15-07-2013 07:12:00 PM", "dd-MM-yyyy HH:mm:ss tt", CultureInfo.InvariantCulture, DateTimeStyles.None);
I am getting the same exception.
Your string format is "dd-MM-yyyy" , but Convert.ToDateTime() default format is "MM-dd-yyyy" . So Options are:
Changing your string format to "07/15/2013"
Forcing the conversion to adapt with it using:
DateTime dt = DateTime.ParseExact("15-07-2013", "dd-MM-yyyy",CultureInfo.InvariantCulture, DateTimeStyles.None);
You can always force the format with DateTime.ParseExact and avoid culture issues with using InvariantCulture:
DateTime dt = DateTime.ParseExact("15-07-2013", "dd-MM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None);
Custom Date and Time Format Strings
However, your code works for me with culture "de-DE".
Update:
You have to use lower hh for the hours when you provide the am/pm designator:
DateTime.ParseExact("15-07-2013 07:12:00 PM", "dd-MM-yyyy hh:mm:ss tt", CultureInfo.InvariantCulture, DateTimeStyles.None)
HH means 24h format which makes no sense at all with the AM/PM designator.
try to use ParseExact:
DateTime dt = DateTime.ParseExact("15-07-2013", "dd-MM-yyyy", null);
The problem why you are getting exception is that by default C# supports date time in this format
"MM/dd/yyyy" where as you are trying passing date time is this format "dd-MM-yyyy"
although you can convert using your format but for that you need to tell the compiler your date is in which format so for that you can use
DateTime
myDate = DateTime.ParseExact("15-07-2013", "dd-MM-yyyy",
System.Globalization.CultureInfo.InvariantCulture);

Error in converting string to datetime in C#.net

I am getting an error in converting string to date in C#.
The error is:
string is not valid datetime.
Below is the code that I am using to convert string into datetime.
string[] DateFormat = { "dd-MM-yyyy", "dd/MM/yyyy", "MM-dd-yyyy", "MM/dd/yyyy" ,"dd-MMM-yy"};
VendorSinceDate = DateTime.ParseExact(dtresult.Rows[0]["VendorSinceDate"].ToString(),DateFormat,System.Globalization.CultureInfo.InvariantCulture,System.Globalization.DateTimeStyles.None);
//dtresult.Rows[0]["VendorSinceDate"].ToString()="06-Jun-12 12:00:00 AM";
None of your possible DateFormats include the time, but the string you're trying to convert does.
You need to add a matching DateFormat, e.g. dd-MMM-yy hh:mm:ss tt

Resources