I want to get the actual time and add one day to it, setter below is datetime column type and it doesn't accept the date function. What should i do?
$password->setExpiretime(date("Y-m-d", time() + 86400));
You may use format method on an instance of DateTime class:
$tomorrow = new DateTime('tomorrow');
$password->setExpiretime($tomorrow->format('Y-m-d'));
Update
According to the comment submitted by the OP, if the return type should be DateTime Object, you may just use:
$tomorrow = new DateTime('tomorrow');
$password->setExpiretime($tomorrow);
Related
Given this code:
var ToDateDate = new String(Request.Form("Comm_ToDateTime"));
ToDateDate = ToDateDate.split("/");
var newToDateTime = ToDateDate[2]+"-"+ToDateDate[1]+"-"+ToDateDate[0]+" "+Request.Form("Comm_ToDateTime_TIME")+":00.000";
CurrentCommunication("Comm_ToDateTime") = newToDateTime;
CurrentCommunication.SaveChanges();
How can i save the date?
This way the date (as Days, month and year) got saved but the hours, minutos does not.
The final output for the newToDateTime variable is 2016-19-09 08:50:00.000
if i use this value (2016-19-09 08:50:00.000) in a barehand SQL update, it works
Turns out that i have to build the Date object with the values and then use getVarDate() mehtod to to pass from a Date Javascriopt object to a record’s date field value which at the end it is the same i was doing :S
Found my answer in this Sage CRM Community
I would like to set hours/minutes/seconds manually in DateTime class from Joda-Time library. What I am trying to do is keep year/month/day value but discard hours/minutes/seconds from date object.
DateTime today = new DateTime();
today.??? (method to call set hours/minutes/seconds to 0)
Happy new year to all!
Immutable Objects
Joda-Time uses immutable objects by default. Rather than call a setter method to change (“mutate”) a member variable, we call a method to generate a new instance based largely on the original.
withTime
To create a new DateTime with a certain time-of-day, call the withTime method.
DateTime now = DateTime.now( DateTimeZone.forID( "America/Montreal" ) );
DateTime lunchtimeToday = now.withTime( 12, 30, 0, 0 ); // Half-past noon.
First Moment Of The Day
If you want midnight (first moment of the day), call withTimeAtStartOfDay. Usually this is 00:00:00.000 but not always.
DateTime todayStart = DateTime.now( DateTimeZone.forID( "America/Montreal" ) ).withTimeAtStartOfDay();
Time Zone Is Crucial
Note that time zone is crucial to determining when a day starts. 'Today' in Paris starts sooner than it does in Montréal.
If you omit the time zone, your JVM’s current default time zone will be applied automatically. Better to specify than rely implicitly on this default.
Use proper time zone names. Avoid the 3 or 4 letter codes that are neither standardized not unique.
I have a nullable variable Start time
Timespan? st=e.StartTime;//Null-able variable;
I am trying to get time in AM/PM format but I am unable to get it.
DateTime date = Convert.ToDateTime(st.ToString());
String f = String.Format("{0:hh:mm:tt}", date);
Error is:
System.FormatException: String was not recognized as a valid DateTime.
If you were to output the results of st.ToString(), you will find that it doesn't contain any date information, only hours, minutes and seconds.
This isn't a valid format for a DateTime, which generally contain date and time information.
You don't need to convert your TimeSpan to a DateTime to format it, you can just use TimeSpan.ToString():
string f = st.Value.ToString(#"hh\:mm\:tt");
For reference: http://msdn.microsoft.com/en-us/library/ee372287.aspx
Also, note the \ before the :, you must do this if you want to include literal strings in the output, as mentioned at the bottom of that documentation page.
Converting a timespan to a date is not possible, a timespan represents x amount of minutes/hours/whatever and you cannot get an exact date from that alone. If you have a date as a starting point, you can add a timespan and that will give you the new date.
st.ToString() will return "System.Nullable<Timespan>" because that is what a nullable type returns - it does not override the default Object.ToString implementation, so returns the type name.
If you want the string of the actual timespan, then you would need to do st.Value.ToString(), but you should be checking for null first (i.e. st.HasValue == true)
Edit: Also see #Sean's comment about how to output the Timespan without converting to a DateTime first.
Edit: Turns out I was slightly wrong - st.ToString() doesn't return the above. So see Sean's answer.
First convert Timespan to Datetime by adding TimeSpan to a base date of 00:00 hrs. Then on that dateTime derive the 12 hr format.
DateTime.Now.Date.Add(OpenTimeSpan).ToString(#"hh\:mm\:tt")
The Accepted Answer is wrong.
You cannot return AM/PM for a TimeSpan because it is only concerned with the length of Time,
not a Time of Day - hench the name, "TimeSpan".
Convert to a DateTime first before converting to a String:
string sTimeOfDay = new DateTime().Add(st).ToString("hh:mm tt");
Note: If your TimeSpan is nullable, then you will need to add Conditional Logic to Handle Nulls and pass in ts.Value instead of ts:
string sTimeOfDay = (st == null ? null : new DateTime().Add(st.value).ToString("hh:mm tt") );
I have to use java.util.Date class as field type in a table.
But I would like to change the display format of the date field with help of joda time (confortable, prefered to use), thats why I want to convert a Date to DateTime.
I know I oversee something, because there is no such a question in stackoverflow :) but I could not find the soulution among the DateTime constructors and so on.
The reverse conversion DateTime.toDate();
exists, but what about the opposite way ?
Thanks for the answers in advance.
Cs
In Vaadin, if you want to change display format in a table without joda, you simply override the method protected String formatPropertyValue(Object rowId, Object colId,
Property property)
Here an example to do it :
Table t = new Table() {
#Override
protected String formatPropertyValue(Object rowId, Object colId,
Property property) {
Object v = property.getValue();
if (v instanceof Date) {
Date dateValue = (Date) v;
return new SimpleDateFormat("yyyy-MMMM-dd").format(dateValue);
}
return super.formatPropertyValue(rowId, colId, property);
}
};
Regards
Éric
Yes, Use Joda-Time
Definitely use Joda-Time or the java.time package in Java 8 (inspired by Joda-Time). The old java.util.Date and java.util.Calendar classes are notoriously troublesome, confusing, and outmoded.
Also, read the Wikipedia pages on UTC and ISO 8601.
Yes, Pass Date To Joda-Time Constructor
➔ Yes indeed, you can pass a java.util.Date object to the constructor of a Joda-Time DateTime object.
The API doc is a bit confusing as this apparently falls into the catch-all version of the constructor taking an java.lang.Object instance. If that Object is in fact a java.util.Date, Joda-Time will extract its millisecond-count-since-epoch and use that number as its own.
Time Zone
A DateTime constructor also assigns a time zone. By default, the JVM’s current default time zone is assigned. I recommend you always pass a desired time zone rather than rely implicitly on the default even if that means calling getDefault.
Example Code
Here is some example code in Joda-Time 2.5 showing how to pass a java.util.Date to a Joda-Time constructor.
java.util.Date date = new java.util.Date();
DateTimeZone zone = DateTimeZone.forID( "America/Montreal" );
DateTime dateTimeMontreal = new DateTime( date , zone );
DateTime dateTimeUtc = dateTimeMontreal.withZone( DateTimeZone.UTC ); // Adjust to another time zone.
Dump to console.
System.out.println( "date: " + date ); // Misleading output. A j.u.Date is in UTC but its toString method applies JVM’s current default time zone.
System.out.println( "dateTimeMontreal: " + dateTimeMontreal );
System.out.println( "dateTimeUtc: " + dateTimeUtc );
When run.
date: Sat Oct 18 18:54:55 PDT 2014
dateTimeMontreal: 2014-10-18T21:54:55.740-04:00
dateTimeUtc: 2014-10-19T01:54:55.740Z
As shown in the Question, to go from a DateTime to java.util.Date, call toDate.
java.util.Date date = dateTimeMontreal.toDate();
SCinitiationtarget.selectedDate = new
Date(rows1[i]['InitiationTarget']);
I am setting my seletedDate in my DateChooser like this. The format i am getting from the Database is 2009-12-30.
Its displaying in correctly.
I believe the date object doesn't recognize the dash as a valid separator. You'll have to some how reformat your date objects.
For example this works:
var date:Date = new Date("2009/12/30");
myDateChooser.selectedDate = date;
But this doesn't:
var date:Date = new Date("2009-12-30");
myDateChooser.selectedDate = date;
For more information on what date formats are valid, see the documentation here: http://livedocs.adobe.com/flex/3/langref/Date.html#Date%28%29
The first argument of Date constructor is called yearOrTimeValue and as its documentation says it accepts either year or time in UTC milliseconds. For proper Date construction use:
new Date(2009, 12, 30)
I got the solution finally.
var dateStr:String = dateFormatter.format(rows1[i]['InitiationTarget']);
SCinitiationtarget.selectedDate = new Date(dateStr);
<mx:DateFormatter id="dateFormatter" formatString="MMM D, YYYY"/>
With this the problem gets solved.
Why not use the parse method of the Date class?
SCinitiationtarget.selectedDate = Date.parse(rows1[i]['InitiationTarget']);