VB6 - "Time-Formatted" Datepicker passes invalid date (below 1/1/1900) - datetime

I have a VB6 Datepicker with the following properties:
Format Type - Time
Min Date - 1/1/1900
Value - 12:00:00 AM
And I initialize it like this:
dtpTimeVal = TimeValue("00:00:00")
However, when I get the date value of the time picker, it returns the value 12/30/1899.
Am I missing any properties or initialization logic here?

there probably is some error where you set the format
the following works:
Private Sub Form_Load()
DTPicker1.Format = dtpTime
DTPicker1.Value = "00:00:00"
End Sub
if i remove the format line, then i get the same result as you do

Related

XPages sessionScope variable and DateTime values

I seem to be losing the value of sessionScope variables between XPages when I load DateTime values from a Notes document (not from the XPage). Here is what I do:
I have an EditBox where the contents are set to type Date only:
<xp:inputText value="#{document1.datum}" id="datum" defaultValue="#{javascript:#Now()}" required="true">
<xp:this.converter>
<xp:convertDateTime type="date"></xp:convertDateTime>
</xp:this.converter>
<xp:dateTimeHelper></xp:dateTimeHelper>
</xp:inputText>
I then save this to a sessionScope variable :
sessionScope.put ("datum", getComponent("datum").getValue());
Then I change XPages by doing a:
var extCont = facesContext.getExternalContext();
extCont.redirect("xpNextPage.xsp")
I then do a sessionScope.get:
print (sessionScope.get ("datum"));
And the contents are fine.
if I do the same thing with a document that I have loaded:
var date:NotesDateTime = doc.getItemValueDateTimeArray("datum");
var start:NotesDateTime = doc.getItemValueDateTimeArray("von");
var dt:NotesDateTime = session.createDateTime (date [0].getDateOnly() + " " + start [0].getTimeOnly());
sessionScope.put ("datum", dt);
then switch to the next page and try and load it with:
print (sessionScope.get ("datum"));
I get a value null.
I have attached a screenshot of the problem (I printed other fields as well so you can see it is only the DateTime fields that are the problem). I do notice that the format of the DateTime value is different... could this be the problem?
NotesDataTime is not serializable, so you cannot store it in the memory. When you use getComponent("datum").getValue(), it returns you Java Date not NotesDataTime. Java date is serializable, so its working.
Try convert your NotesDataTime to Java Date.
dt.toJavaDate()

plone.app.event- Using IEventBasic as a behavior, how can I properly set the end field programatically in an event?

I have a dexterity content type (my.product.myobject) that uses the IEventBasic behavior (implemented in the xml file with other behaviors) and I'm trying to make the end field be set within the same week of the start field. I attempt to correct the end date in an IObjectAddedEvent (zope.lifecycleevent.interfaces.IObjectAddedEvent).
I first get the week range from the start field:
def getWeekRangeFromDate(a_date,offset=1):
start = (a_date - timedelta(days=(a_date.weekday() + offset) % 7)).replace(hour=0,minute=0)
end = (start + timedelta(days=6)).replace(hour=23,minute=59)
return {'start':start,
'end':end,
}
def myObjectAdded(myObject, event):
week_range = getWeekRangeFromDate(myObject.start)
if myObject.end > week_range['end']:
myObject.end = week_range['end']
I have the end field printed in the browserview and I use IEventAccessor to extract the end date from the myObject:
class View(BrowserView):
def __init__(self,context,request):
...
self.context = self.context
def getFormattedEnd(self):
if self.context.whole_day == False:
return IEventAccessor(self.context).end.strftime('%m/%d %I:%M %p')
...
When it doesn't need to be programmatically corrected, the end field displays correctly, but when it does, it appears 5 hours off.
In my myObjectAdded event, I tried:
if myObject.end > week_range['end']:
myObject.end = week_range['end'] - timedelta(hours = IEventAccessor(myObject).end.offset().total_seconds()/3600)
This does appear right actually, but when I go to the edit form and change the start field, the end field ends up changing itself seemingly random. Changing the starting hour field to 16 changed the end's month about two weeks ahead.
How can I set it without it acting up? Am I misundertanding how to use IEventBasic?
Edit: I'm coming across something really interesting. In the edit form, the Start End Validator is failing.
Event Starts - 25/November/2016 9:00
Event Ends - 25/Novermber/2016 20:00
I click submit and the status message says End date must be after the start date.
Edit: The version of Plone I am using is 4.3.

Changing Only Time in DateTime Format - MVC 4

I've been working with the following Lynda.com tutorial on learning MVC 4 and Razor. I'm stuck on trying to have the time that's displayed only shows the hours, minutes, then AM/PM. As of now, the screen still includes the seconds (as seen below):
I tried formatting my dates like this post about DateTime, which didn't work. Now I have the following code within my function in the controller section entitled "AuctionsController.vb", similar to this post:
Function Auction() As ActionResult
Dim mainauction = New MVCAuction3.Models.Auctions
Dim ts As New TimeSpan(10, 0, 0)
mainauction.Title = "Example Auction"
mainauction.Description = "This is an example Auction"
mainauction.StartTime = DateTime.Now + ts
mainauction.EndTime = DateTime.Now.AddDays(7) + ts
mainauction.StartPrice = 1.0
mainauction.CurrentPrice = Nothing
ViewData("Auction") = mainauction
Return View()
End Function
This is how Razor is displaying the content from the view "Auction.vbhtml":
<p>Start Time: #auction.StartTime.ToString() </p>
<p>End Time: #auction.EndTime.ToString()</p>
<p>Starting Price: #FormatCurrency(auction.StartPrice.ToString())</p>
Edit(s):
This is how I declared my time variables in my modal file:
Private Property x_StartTime As DateTime
Private Property x_EndTime As DateTime
Public Property StartTime() As DateTime
Get
Return x_StartTime
End Get
Set(value As DateTime)
x_StartTime = value
End Set
End Property
Public Property EndTime() As DateTime
Get
Return x_EndTime
End Get
Set(value As DateTime)
x_EndTime = value
End Set
End Property
I've also tried to have it from within the "Auction.vhtml" view the following, which unfortunately gave me the server error indicating the "Input string was not in a correct format.":
<p>Start Time: #auction.StartTime.ToString("g") </p>
<p>End Time: #auction.EndTime.ToString("g")</p>
<p>Starting Price: #FormatCurrency(auction.StartPrice.ToString())</p>
What am I doing wrong in either the Razor or MVC code that is not formatting the time? Any help is greatly appreciated!
You should take a look at Custom Date and Time Format Strings on MSDN. Basically, you can pass a format string to the ToString method of your DateTime objects.
Here's a sample that omits the seconds:
auction.StartTime.ToString("M/d/yyyy hh:mm tt")
I would highly suggest looking into the DisplayFormat (MSDN) attribute. You would append it to your models StartTime property like so
[DisplayFormat(ApplyFormatInEditMode=true, DataFormatString = "{0:M/d/yyyy hh:mm tt}")]
public DateTime StartTime {get;set;}
and then you would output the information in your view by the DisplayFor function:
#Html.DisplayFor(x=> x.StartTime)

Issue in converting Timespan variable to 12 hour format

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") );

Flex: Converting DateField text to seconds?

Is there anything wrong with following snippet of code?
var d:Date = DateField.dateToString(myDateField.text,"DD/MM/YYYY");
testTextArea.text = d.getSeconds().toString();
Error: Implicit coercion of a value of
type String to an unrelated type Date.
Here is your problem: DateField.dateToString's first parameter is supposed to be a date. It then takes that date and returns a string using the second parameter as a format string.
It looks like you're trying to convert the string to a date (the other way around) so you can get the seconds from it and put it in the text area. The DateField control has a selectedDate parameter that will give you the date you need. Then you just run this code to put it in the text area:
testTextArea.text = myDateField.selectedDate.getSeconds().toString();

Resources