How to remove time from date and time - c#-4.0

In DOB i am getting date and time but i need only date
foreach (DataRow row in ProfileDt.Rows)
{
UserProfileData.FirstName = row["FirstName"].ToString();
UserProfileData.LastName = row["LastName"].ToString();
UserProfileData.Email = row["Email"].ToString();
UserProfileData.Address = row["HouseNo"].ToString();
UserProfileData.City = row["CityName"].ToString();
UserProfileData.date = row["DOB"].ToString();
}

If your database is giving you a string then you'll need to parse it to a date then back to a string with the desired date format.
string dateFormat = "MM/DD/YYYY HH:mm:ss" // REPLACE WITH THE CORRECT FORMAT
UserProfileData.date = DateTime.ParseExact(row["DOB"].ToString(), dateFormat ,
System.Globalization.CultureInfo.InvariantCulture).ToString("MM/DD/YYYY");
If your database is giving you a datetime directly (which is the better solution) then it becomes even easier.
UserProfileData.date = ((DateTime)row["DOB"]).ToString("MM/DD/YYYY");

Convert your string to DateTime type and use Date property, something like yourDate.Date
You can use DateTime.Parse or DateTime.ParseExact for parsing string to DateTime
It also make sense to return DateTime type directly from database so you do not need any conversions...

Related

Is there any easy and efficient way to write this code block which I am using to convert the Date and time to Timestamp in Groovy

String date = parameters.sendDate;
String time = parameters.sendTime;
SimpleDateFormat date12Format = new SimpleDateFormat("hh:mm a");
SimpleDateFormat date24Format = new SimpleDateFormat("HH:mm");
String convertedTime = date24Format.format(date12Format.parse(time));
String convertedDate = UtilDateTime.toTimestamp(date , convertedTime);
//Currently this one is working but is there any much compact and easy way?
You should be able to do it with much less code. I used 11PM to illustrate getting it to 24 hour format:
String sometime = '11:55 PM'
assert Date.parse('hh:mm a', sometime).format("HH.mm") == '23.55'

split date month and year in different fields

I have a date stored in my database in this format:
2013-01-08T17:16:36.000Z
I have searched for it a lot but it shows how to get this date format but what I am looking for is want to separate this like:
a=yyyy,b=dd and c=mm
And the rest is not required because I want to show date, month and year all this three in different fields.
I want to store the date in one variable, month in another variable and year in another one.
I tried this https://jsfiddle.net/ccywo7a9/ and actually works but i want this using split in node.js
This works
var Date='2013-01-08T17:16:36.000Z';
var b=Date.slice(0,10);
var c=b.split('-');
var e=c[Symbol.iterator]();
console.log(e.next().value);
console.log(e.next().value);
console.log(e.next().value);
and gives the value in three different variables.
Output:
2013
01
08
Try this method.. it's convert Mongo DB date and Time format...
String dateParts[] = "2013-01-08T17:16:36.000Z".split("T");
String split_date = dateParts[0];
String sp_time = dateParts[1];
System.out.println("sp_date----"+split_date);
String dateParts2[] = sp_time.split("Z");
String split_time = dateParts2[0];
System.out.println("sp_time----"+split_time);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-ddhh:mm:ss.SSS");
SimpleDateFormat cnv = new SimpleDateFormat("MMM dd hh:mm a");
Date modified_date = null;
try {
modified_date = format.parse(split_date+split_time);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("DATE----------------------"+cnv.format(modified_date));
Result :Jun 06 10:58 AM

How to check if something is being returned as a string or a date as string?

In my app the api can return strings or dates as strings. I need to check if the api returns a date first. if it is not a date, just populate the field with the string. If it is a date, do the conversion before populating the field. The conversion and the populate the field part I have figured out, but how can I tell if it's returning a date as a string or a string? This is what the api returns:
data?.data?.nextAvailability
So I need it be something like
if data?.data?.nextAvailability is a string {
// do something
} else if data?.data?.nextAvailability is a date as a string {
// do this
}
You could maybe do something like this:
let string = data?.data?.nextAvailability
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
guard let date = dateFormatter.date(from: string) else {
print("not a date") // And do what you need to do
return
}
// is a date... do what you need to do with a date.
print(date)

Converting String to datefield / Java Microedition

Using J2ME, netbeans 7.2, Developing a mobile app..
I have converted the Datefield value to a String and Now want to put it back to a Datefield. To do this I need to convert the String back to Datefield, I am using the following code but its not happening.
long myFileTime = dateField.getDate().getTime(); // getting current/set date from the datefield into long
String date = String.valueOf(myFileTime); // converting it to a String to put it back into a different datefield
Date updatedate= stringToDate(date); // passing the string 'date' to the Method stringToDate() to convert it back to date.
dateField1.setDate(updatedate); // updating the date into the new datefield1
public Date stringToDate(String s)
{
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(s.substring(0, 2)));
c.set(Calendar.MONTH, Integer.parseInt(s.substring(3, 5)) - 1);
c.set(Calendar.YEAR, Integer.parseInt(s.substring(6, 10)));
return c.getTime();
}
Since you have mentioned that you have the long myFileTime around, you should be able to use:
Date updatedate=new Date(myFileTime);
To convert back to your date. If only your String is available, you should modify your function to this:
public Date stringToDate(String s){
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(s.substring(0, 2)));
c.set(Calendar.MONTH, Integer.parseInt(s.substring(2, 4))-1 );
c.set(Calendar.YEAR, Integer.parseInt(s.substring(4, 8)));
return c.getTime();
}
Note the changed indexes.
In Java SE, you should be able to use the following line, instead of setting each fields separately:
c.setTimeInMillis(Long.parseLong(s));
Since in s you have the dateField.getDate().getTime() that is equal to myFileTime, the number of seconds starting with January 1, 1970, based on your provided code.
Your stringToDate should work only if your string will have the following format: ddMMyyyy. Also note that in this case you should use a SimpleDateFormat to parse, something like:
Date updatedate = new java.text.SimpleDateFormat("ddMMyyyy HH:mm:ss").parse(date);

convert strings to datetime

I have asp.net application and am using Entity Framework to connect it with the database. in this application, I have a textbox to get the date(am using calender css style here), and its in the string type.
I have a column in my database and its in Date Time format, I need to compare the textbox value with the date column in my database, for this I just used the code as
public StudentAttendances(string date)
{
if (date != "")
{
DateTime date1 = Convert.ToDateTime(date);
foreach (DataAccess.StudentAttendance studentAttendance in buDataEntities.StudentAttendances.Where(s => s.Date == date1))
{
this.Add(new StudentAttendance(studentAttendance.StudentId));
}
}
}
for example if I select a date in my textbox(the format is 04/05/2012) and when I compare this with the database its not showing any data, but actually some datas are there for this date.
Your code is comparing both day and time (hours, minutes etc will have to match). Try comparing just the day part like this:
buDataEntities.StudentAttendances.Where(s => s.Date.Subtract(date1).Days == 0)
I also think that you should specify what format the input date from the users is in.
04/05/2012 may mean both 4th April or 5th of May depending on your computers regional setting.
Here is an example (below) for converting a date string in American format to DateTime object:
DateTime date1 = DateTime.Parse(date, new CultureInfo("en-US"));
Hope that helps!
your ask is very limited, but try to see this
http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html
Please check whether the following works for you:
public StudentAttendances(string date)
{
if (date != "")
{
// please see the change from your given code
DateTime date1 = DateTime.ParseExact(date, "MM/dd/yyyy",
System.Globalization.CultureInfo.InvariantCulture);
foreach (DataAccess.StudentAttendance studentAttendance in buDataEntities.StudentAttendances.Where(s => s.Date == date1))
{
this.Add(new StudentAttendance(studentAttendance.StudentId));
}
}
}

Resources