how to display current time with chosen timezone? - asp-classic

how to display current time with chosen timezone instead of server TZ? (VB)

to use any TZ; simply use this row to add hours:
anytime = DateAdd("H", HOUR_DIFFERENCE, Now())

Public Declare Function GetTimeZoneInformation Lib "kernel32" Alias
"GetTimeZoneInformation" (lpTimeZoneInformation As
TIME_ZONE_INFORMATION) As Long
Public Type TIME_ZONE_INFORMATION
Bias As Long
StandardName(32) As Integer
StandardDate As SYSTEMTIME
StandardBias As Long
DaylightName(32) As Integer
DaylightDate As SYSTEMTIME
DaylightBias As Long
End Type
GMT = systemtime + bias

Related

Turn off timezone DST adjustment in VB.NET?

fellow programmers. Recently i got a problem when the DST applied to my asp.net application.
Originally, i got my datetime converter as follow:
Private Function ConvertTimezone(convertDatetime As DateTime, zoneID As String) As DateTime
Dim timeZoneInfo As TimeZoneInfo = System.TimeZoneInfo.FindSystemTimeZoneById(zoneID)
Dim dataTimeByZoneId As DateTime = System.TimeZoneInfo.ConvertTime(convertDatetime, System.TimeZoneInfo.Local, timeZoneInfo)
Return dataTimeByZoneId
End Function
Which is running smoothly as i am expected. However, when daylight saving started, all stuff seems to went to the wrong way AS I LIST THE OPTION TO SELECT AS UTC - 12 TO UTC + 12 by getting the standard time and convert it using the above code.
For example, the problem i am facing is the shift of hours, before DST, My UTC - 5 is from Atlantic standard time ,but after DST it returns UTC - 4 now as .NET CONVERT IT BY ITSELF. The dropdown has gone wrong since then.
Is there anyway to turn the DST adjustment off? or any other work around can complement the offset? (No other library is allowed ,sorry fellows..)
I worked it around by parsing the UTC + x value where x is the offset
Private Function ConvertTimezone(convertDatetime As DateTime, zoneID As String) As DateTime
Dim desttimeZoneInfo As TimeZoneInfo
Dim dataTimeByZoneId As DateTime
If Not zoneID.Contains("GMT") Then
desttimeZoneInfo = System.TimeZoneInfo.FindSystemTimeZoneById(zoneID)
dataTimeByZoneId = System.TimeZoneInfo.ConvertTime(convertDatetime, System.TimeZoneInfo.Local, desttimeZoneInfo)
Else
dataTimeByZoneId = System.TimeZoneInfo.ConvertTimeToUtc(convertDatetime, System.TimeZoneInfo.Local)
dataTimeByZoneId = dataTimeByZoneId.AddHours(Double.Parse(zoneID.Substring(3)))
End If
Return dataTimeByZoneId
End Function

Creating DateTime object in Visual C++

I am familiar with creating DateTime object based on current system time
DateTime dtObject = DateTime::Now;
My question is: if I have integer variables iHour, iMinute and iSecond, how do I "put them back" into a DateTime object?
I hope I made myself clear. Thanks in advance!
Something along the lines of
DateTime today = DateTime::Today;
DateTime todayWithTime(today.Year, today.Month, today.Day,
hour, minute, second);
Another approach:
DateTime todayWithTime = DateTime::Today + TimeSpan(hour, minute, second);

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)

Is there a better way to encode a (MS) DateTime as an Int?

BACKGROUND (ok to ignore/skip):
I feel like there should be a better way to do what I'm doing, but I don't know what it is and I think my solution works, but I thought I'd ask in case there is something more elegant or faster or whatever.
I am developing a web page with MS Razor MVC which uses Html.DropDownList, which gives a pick list UI control which maps choices as display strings to integer ID codes. (I don't think I can have it map strings to DateTime values.)
But in a couple of cases, I want to choose from dates that are stored as DateTime objects. I realize I could make yet another table in memory that relates ID codes to DateTime values, but I thought instead (and also because I think I may want to encode dates as ints for yet another workaround of web page annoyances), I would encode the date as an int.
PROBLEM:
How best to convert the date of a DateTime object as an int value, and then later set a DateTime's date back to that date from the encoded int. The main ugliness of my solution is that DateTime provides a read-only DayOfYear function, but no way I know of to set a date back to (Date, DayOfYear), so I wrote a method with a loop, which is cute but probably slowish.
MY CURRENT (OK) SOLUTION:
public int encodeDate(DateTime date)
{
return ((date.Year - 2012) * 400) + date.DayOfYear;
}
public DateTime decodeDateCode(int dateCode)
{
int year = (dateCode / 400) + 2012;
int day = dateCode % 400;
// MS DateTime doesn't provide a direct reverse conversion for DayOfYear, so find it:
int month = 1;
int mThresh = DateTime.DaysInMonth(year, month);
while (day > mThresh)
{
day -= mThresh;
month++;
mThresh = DateTime.DaysInMonth(year, month);
}
DateTime dateValue = new DateTime(year, month, day);
return dateValue;
}
Ho about to format the timestamp as POSIX time (POSIX time / Unix time, see http://en.wikipedia.org/wiki/Unix_time)?
I had a similar problem myself and found a great solution here: Convert a Unix timestamp to a .NET DateTime
From the link:
DateTime ConvertFromUnixTimestamp(double timestamp)
{
}
double ConvertToUnixTimestamp(DateTime date)
{
}

How can I convert java.util.Date to org.joda.time.DateTime?

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

Resources