How do I get browser timezone using Flex/AS3? - apache-flex

How do I get client browser's timezone in Flex/AS3?

Have a look on the following: http://thanksmister.com/2011/10/06/determining-local-timezone-in-actionscript-air-flex-as3/
It may help.

Can you use the timeZoneOffest of the Date object?
Honestly, I believe this just passes back information based on the user's OS settings. I wouldn't have expected a bBrowser to actually have a time zone.

I've been wanting the same thing, but I found nothing on the Internet. I was already using FlexDateUtils (blog post), but it only formats timezones as 'GMT -600'. Luckily, I can safely say that the users of my app will be in the United States, so I modified to 'DateUtils.buildTimeZoneDesignation(date:Date)' as follows. I hope this helps.
private static function buildTimeZoneDesignation( date:Date ):String {
if ( !date ) {
return "";
}
var timezoneOffsetHours:Number = date.getTimezoneOffset() / 60;
// custom timezone handlers (assumes major U.S. zones with daylight savings time dates as of 2011)
if (3 < timezoneOffsetHours && timezoneOffsetHours < 12)
{
var usDST:Boolean = false;
// the date of the Sunday before today unless today is Sunday
var sundayBeforeToday:Number = date.date - date.day;
if (2007 <= date.fullYear) {
// test for since-2007 extended daylight savings time
if (2 < date.month && date.month < 10) {
// daylight savings time (April through October)
usDST = true;
}
else if (date.month == 2) {
// DST starts second Sunday in March
usDST = (7 < sundayBeforeToday);
}
else if (date.month == 10) {
// DST ends first Sunday in November
usDST = (0 < sundayBeforeToday);
}
}
else {
// test for pre-2007 daylight savings time
if (3 < date.month && date.month < 9) {
// daylight savings time (May through September)
usDST = true;
}
else if (date.month == 3) {
// DST starts first Sunday in April
usDST = (0 < sundayBeforeToday);
}
else if (date.month == 9) {
// DST ends last Sunday in October
usDST = (sundayBeforeToday + 7 <= 31);
}
}
// return custom timezone strings for US timezones
switch (timezoneOffsetHours) {
case 4:
// Eastern or Atlantic
return usDST ? "EDT" : "AST";
case 5:
// Central or Eastern
return usDST ? "CDT" : "EST";
case 6:
// Mountain or Central
return usDST ? "MDT" : "CST";
case 7:
// Pacific or Mountain
return usDST ? "PDT" : "MST";
case 8:
// Alaska or Pacific
return usDST ? "AKDT" : "PST";
case 9:
// Hawaii or Alaska
return usDST ? "HADT" : "AKST";
case 10:
// Samoa or Hawaii
return usDST ? "SDT" : "HAST";
case 11:
if (!usDST)
// Samoa
return "SST";
break;
}
}
// else just generate a GMT string
var timeZoneAsString:String = "GMT ";
// timezoneoffset is the number that needs to be added to the local time to get to GMT, so
// a positive number would actually be GMT -X hours
if ( 0 < timezoneOffsetHours && timezoneOffsetHours < 10 ) {
timeZoneAsString += "-0" + ( timezoneOffsetHours ).toString();
} else if ( date.getTimezoneOffset() < 0 && timezoneOffsetHours > -10 ) {
timeZoneAsString += "0" + ( -1 * timezoneOffsetHours ).toString();
}
// add zeros to match standard format
timeZoneAsString += "00";
return timeZoneAsString;
}

Related

Wrong date with moment JS plugins

Trying to generate 15 days advance date using moment JS but it doesnt starts with today's date but the day before.
console.log( "Today's date : " + d1.getDate().toString())
for (i = 1; i <= 15; i++) {
console.log(moment(d1).day(i, 'd').format('MM/DD/YYYY') )
selectDt.append($("<option></option>")
.attr("value", moment(d1).day(i,'d').format('MM/DD/YYYY'))
.text(moment(d1).day(i,'d').format('MMM-DD,YYYY'))
.prop("selected", (selval != undefined) ? (moment(d1).day(i,'d').format('MMM-DD,YYYY') == selval) : '')
)
}
Result is,
My solution is to use the moment add method for getting after 15 dates. If you want before 15 use the moment subtract method.
For your code with my changes.
console.log( "Today's date : " + d1.getDate().toString())
for (i = 0; i <= 15; i++) {
console.log(moment(d1).add(i, 'd').format('MM/DD/YYYY') )
selectDt.append($("<option></option>")
.attr("value", moment(d1).add(i,'d').format('MM/DD/YYYY'))
.text(moment(d1).add(i,'d').format('MMM-DD,YYYY'))
.prop("selected", (selval != undefined) ? (moment(d1).day(i,'d').format('MMM-DD,YYYY') == selval) : '')
)
}
My working code
var d1 = new Date();
console.log( "Today's date : " + d1.getDate().toString())
for (i = 0; i <= 15; i++) {
console.log(moment(d1).add(i, 'day').format('MM/DD/YYYY') )
}
My Output

How to add surcharge

I have this process, the user selects 2 dates. The system will then check if there are weekends. Weekends are:
Friday
Saturday
Sunday
Every time there are weekends, there would be additional charge + $10 PER DAY OF THE WEEKEND.
I have this code below, what I want is the process to add the surcharge.
http://jsfiddle.net/xtD5V/71/
function isWeekend(date1, date2) {
var d1 = new Date(date1),
d2 = new Date(date2),
isWeekend = false;
while (d1 < d2) {
var day = d1.getDay();
isWeekend = (day == 6) || (day == 0);
if (isWeekend) { return true; }
d1.setDate(d1.getDate() + 1);
}
return false;
}
alert(isWeekend(date1, date2));
You can opt to try this option.
Loop through all the dates and check for weekends like you did.
Store it in a variable
Option 1:
function noOfWeekends(date1, date2) {
//record number of weekends
var no_of_weekends = 0;
var d1 = new Date(date1),
d2 = new Date(date2),
isWeekend = false;
while (d1 < d2) {
var day = d1.getDay();
isWeekend = (day == 6) || (day == 0);
if (isWeekend) { no_of_weekends++; }
d1.setDate(d1.getDate() + 1);
}
return no_of_weekends;
}
//noOfWeekends now returns the number of weekends. You can do more stuff with it
alert(noOfWeekends(date1, date2) > 0);
alert("surcharge: " + noOfWeekends * 10);
*P/s: You can opt to write a formula to calculate the number of weekends without a loop since we can derive the information from your current day and number of days left. Do give it a try.

How to check current time is 12 noon in c#?

i need to check current time is 12 noon using c# and timer tick event is used to execute the code
int secs = System.DateTime.Now.Second;
int hours = System.DateTime.Now.Hour;
int minutes = System.DateTime.Now.Minute;
string AmPm = DateTime.Now.ToString("tt");
if (Convert.ToInt32("12") == hours && Convert.ToInt32("0") == minutes && Convert.ToInt32("0") == secs && AmPm.ToUpper().Trim() == "PM")
{
}
else
{}
You should just be able to do:
var is_noon = (
DateTime.Now.Hour == 12 &&
DateTime.Now.Minute == 0 &&
DateTime.Now.Second == 0
);
No need to convert strings to ints, you're using literal values anyhow.
However as Hans Kesting pointed out, depending on the Timer you're using there is a chance for inaccuracy so you'd want some leniency, personally, I'd drop checking for seconds.

Calculating time in ASP.NET [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I calculate relative time?
While posting something on SO it shows posted 5 mins ago or 1hr ago. How do I calculate the time and display it the same way?
For example C# Pretty Date Formatting:
static string GetPrettyDate(DateTime d)
{
// 1.
// Get time span elapsed since the date.
TimeSpan s = DateTime.Now.Subtract(d);
// 2.
// Get total number of days elapsed.
int dayDiff = (int)s.TotalDays;
// 3.
// Get total number of seconds elapsed.
int secDiff = (int)s.TotalSeconds;
// 4.
// Don't allow out of range values.
if (dayDiff < 0 || dayDiff >= 31)
{
return null;
}
// 5.
// Handle same-day times.
if (dayDiff == 0)
{
// A.
// Less than one minute ago.
if (secDiff < 60)
{
return "just now";
}
// B.
// Less than 2 minutes ago.
if (secDiff < 120)
{
return "1 minute ago";
}
// C.
// Less than one hour ago.
if (secDiff < 3600)
{
return string.Format("{0} minutes ago",
Math.Floor((double)secDiff / 60));
}
// D.
// Less than 2 hours ago.
if (secDiff < 7200)
{
return "1 hour ago";
}
// E.
// Less than one day ago.
if (secDiff < 86400)
{
return string.Format("{0} hours ago",
Math.Floor((double)secDiff / 3600));
}
}
// 6.
// Handle previous days.
if (dayDiff == 1)
{
return "yesterday";
}
if (dayDiff < 7)
{
return string.Format("{0} days ago",
dayDiff);
}
if (dayDiff < 31)
{
return string.Format("{0} weeks ago",
Math.Ceiling((double)dayDiff / 7));
}
return null;
}
(And you can also use a jquery plugin named Prettydate.)

Can this date time function be written more efficiently?

The following function returns the difference between two date time values in words (as a string). Can it be written more efficiently/elegantly?
/**
* #hint Returns the difference between two time strings in words.
*/
public string function timeAgoInWords(required date fromTime, date toTime=now())
{
local.secondDiff = dateDiff("s", arguments.fromTime, arguments.toTime);
if (local.secondDiff <= 60)
return "#local.secondDiff# seconds ago";
local.minuteDiff = dateDiff("n", arguments.fromTime, arguments.toTime);
if (local.minuteDiff <= 60)
if (local.minuteDiff < 2)
return "1 minute ago";
else return "#local.minuteDiff# minutes ago";
if (local.minuteDiff <= 1440)
if (local.minuteDiff <= 120)
return "1 hour ago";
else return "#int(local.minuteDiff/60)# hours ago";
if (local.minuteDiff <= 2880)
return "yesterday";
if (local.minuteDiff <= 4320)
return "2 days ago";
local.monthDiff = dateDiff("m", arguments.fromTime, arguments.toTime);
if (local.monthDiff <= 12)
return "#dateFormat(arguments.fromTime, "mmm dd")# at #timeFormat(arguments.fromTime, "h:mm")#";
return "#dateFormat(arguments.fromTime, "mmm dd 'yy")# at #timeFormat(arguments.fromTime, "h:mm")#";
}
This is what I wrote a few months ago, based on the UDF Al Everett posted above in the comment and written in CF9 script style. It won't be more efficient. In fact, it should be slower then your implementation 'cause it has multiple calls to dateDiff(), and needs to set up 2 arrays up front, but the overall line count is shorter and easily understandable.
string function ago(required Date dateThen)
{
var dateparts = ["yyyy","m","d","h","n"];
var datepartNames = ["year","month","day","hour","minute"];
var rightNow = Now();
for (var i = 1; i <= 5; i++) // 5 == arrayLen(dateparts)
{
var diff = dateDiff(variables.dateparts[i], dateThen, rightNow);
if (diff > 1)
return "#diff# #datepartNames[i]#s ago";
if (diff == 1)
return "#diff# #datepartNames[i]# ago";
}
return "Just Now";
}
It looks good to me. You could instead use your first diff (local.secondDiff) for all your tests rather than re-diffing, but this is probably easier to read.

Resources