Display time with different timezone giving wrong value using moment - momentjs

I want to display time with 3 different time zone.
eg. Considering "America/Mexico_City" as standard.
1. "America/New_York" is 1 hr ahead of Mexico_City
2. "America/Los_Angeles" is 2 hrs behind of Mexico_City
But I am getting exactly opposite o/p -> New York showing 1 hr behind && Los Angeles is 2 hrs ahead.
o/p ->
Mexico_City-> normal ->Jun 11, 2018 19:12 PM
New_York-> 1 hr ->Jun 11, 2018 18:12 PM
Los_Angeles-> 2hr ->Jun 11, 2018 21:12 PM
var date = "2018-06-11T14:12:43";
var LastModifiedDate = {
date: date,
tzId: "America/Mexico_City",
tzCode: "PDT"
}
console.log("Mexico_City-> normal ->" + utils.formatDate(utils.convertPlatformDateToMillis(LastModifiedDate)));
LastModifiedDate = {
date: date,
tzId: "America/New_York",
tzCode: "PDT"
}
console.log("New_York-> 1 hr ->" + utils.formatDate(utils.convertPlatformDateToMillis(LastModifiedDate)));
LastModifiedDate = {
date: date,
tzId: "America/Los_Angeles",
tzCode: "PDT"
}
console.log("Los_Angeles-> 2hr ->" + utils.formatDate(utils.convertPlatformDateToMillis(LastModifiedDate)));
class Utils {
convertPlatformDateToMillis(pltDateObject) {
return momentTimeZone.tz(pltDateObject.date, pltDateObject.tzId);
}
formatDate(dateInMillis) {
return moment(new Date(dateInMillis)).format('MMM DD, YYYY HH:mm A');
}
}

var newYork = momentTimeZone.tz(moment(), "America/New_York");// Jun 12, 2018 10:00 AM
var losAngeles = newYork.clone().tz("America/Los_Angeles"); //Jun 12, 2018 07:00 AM
var mexico = newYork.clone().tz("America/Mexico_City"); // Jun 12, 2018 09:00 AM

Related

How to Generate Date

I have a form and on a button click I automatically generate a date exactly a year in the future. I would like to know how to make sure that this date is not on a public holiday or on a weekend. Any help please ?
I would like to store the value of the date inside a variable in order to just place it inside the command.Parameters.AddWithValue("#DueDate",___________)
You can create your own logic for this. It's quite simple. Create a method that check if the date is a weekday or a holiday. But you have to hard-code the holidays since they differ per country/continent/culture etc.
public bool IsWeekday(DateTime date)
{
int dayOfWeek = (int)date.DayOfWeek;
//week starts on sunday
if (dayOfWeek == 0 || dayOfWeek == 6)
{
return false;
}
else
{
return true;
}
}
public bool IsHoliday(DateTime date)
{
int currentYear = DateTime.Now.Year;
//define your holidays here, they differ between cultures and continents etc
List<DateTime> holidays = new List<DateTime>()
{
new DateTime(currentYear, 1, 1), //new years day
new DateTime(currentYear, 1, 9), //for testing
new DateTime(currentYear, 4, 27), //kings day
new DateTime(currentYear, 6, 21), //longest day of the year
new DateTime(currentYear, 12, 25), //christmas
new DateTime(currentYear, 12, 26) //christmas
};
//check the date against the list of holidays
if (holidays.Any(x => x == date.Date))
{
return true;
}
else
{
return false;
}
}
Now you can check if it works.
//get a monday
DateTime monday = new DateTime(2019, 1, 7);
//loop all days of the week
for (int i = 0; i < 7; i++)
{
DateTime nextDay = monday.AddDays(i);
Label1.Text += string.Format("{0} - {1} - {2}<br>", nextDay.ToLongDateString(), IsWeekday(nextDay), IsHoliday(nextDay));
}
Result of the above loop
maandag 7 januari 2019 - True - False
dinsdag 8 januari 2019 - True - False
woensdag 9 januari 2019 - True - True
donderdag 10 januari 2019 - True - False
vrijdag 11 januari 2019 - True - False
zaterdag 12 januari 2019 - False - False
zondag 13 januari 2019 - False - False

getting the date of monday and friday from next week

I have the following method that I use to get the date for Monday and Friday of next week.
For example, today is 1/6/2017. If I ran it, I would hope to get the following results:
monday = 1/9/2017
friday = 1/13/2017
The method works fine if I run it earlier in the week, but if I run it later like a friday or saturday, it returns the monday and friday dates 2 weeks from now(not next week).
For example, running it today(Friday the 6th), I get the following results:
monday = 1/16/2017
friday = 1/20/2017
Here is the method:
public static DateTime NextWeekRange(DateTime start, DayOfWeek day)
{
var add_days = ((int)day - (int)start.DayOfWeek + 7) % 7;
return start.AddDays(add_days);
}
And I call it like this:
var monday = NextWeekRange(DateTime.Today.AddDays(i_today), DayOfWeek.Monday);
var friday = NextWeekRange(DateTime.Today.AddDays(i_today + 4), DayOfWeek.Friday);
I'm not quite sure what I got wrong, so another pair of eyes would help!
Thanks!
DateTime MyDate = DateTime.Now;
DateTime NextMondayDate;
DateTime NextFridayDate;
Boolean Test = true;
while (Test)
{
if (MyDate.DayOfWeek.ToString().ToUpper() == "MONDAY")
{
NextMondayDate = MyDate;
NextFridayDate = MyDate.AddDays(4);
Test = false;
}
else if (MyDate.DayOfWeek.ToString().ToUpper() == "FRIDAY")
{
NextFridayDate = MyDate;
NextMondayDate = MyDate.AddDays(3);
Test = false;
}
else
{
MyDate = MyDate.AddDays(1);
}
}

Formatting Date in Spreadsheet

I have a column in spreadsheet that is 'date'.
When I retrieve the date with the following code,
for (var i = 1; i < data.length; ++i) {
var row = data[i];
var date= row[0];
Logger.log(date);
I get
Wed Nov 12 2014 00:00:00 GMT+0800 (HKT)
Is there a way to just show the output as
Wed Oct 15 2014
I tried converting to JSON since it is an object, so that I can use substring
startDate = JSON.stringify(startDate);
startDate = startDate.substring(0, 14);
But it doesn't output correctly.
"2014-11-12T16:00:00.000Z"
Use formatDate() from the "Utilities" class:
var formattedDate = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd'T'HH:mm:ss'Z'");
https://developers.google.com/apps-script/reference/utilities/utilities#formatDate(Date,String,String)
Did you try using "formatDate" in Utilities class
formatDate(date, timeZone, format)
try doing this:
var dateToFormat = row[0];
var formattedDate = Utilities.formatDate(new Date(dateToFormat), "GMT", "EEE, MMM d, yyyy");
Logger.log(formattedDate);

How to convert a date string "dd/MM/yyyy" format into "MM/dd/yyyy" in asp.net c#?

I want to convert a string date formate "dd/MM/yyyy" into "MM/dd/yyyy" in c#
example
string d ="25/02/2012"; i want to convert into 02/25/2012
You can parse it to DateTime object using DateTime.ParseExact and later use ToString("MM/dd/yyyy")to display theDateTime` object like.
string d ="25/02/2012";
DateTime dt = DateTime.ParseExact(d, "d/M/yyyy", CultureInfo.InvariantCulture);
// for both "1/1/2000" or "25/1/2000" formats
string newString = dt.ToString("MM/dd/yyyy");
Make sure to include using System.Globalization; at the top.
DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 123);
String.Format("{0:y yy yyy yyyy}", dt); // "8 08 008 2008" year
String.Format("{0:M MM MMM MMMM}", dt); // "3 03 Mar March" month
String.Format("{0:d dd ddd dddd}", dt); // "9 09 Sun Sunday" day
String.Format("{0:h hh H HH}", dt); // "4 04 16 16" hour 12/24
String.Format("{0:m mm}", dt); // "5 05" minute
String.Format("{0:s ss}", dt); // "7 07" second
String.Format("{0:f ff fff ffff}", dt); // "1 12 123 1230" sec.fraction
String.Format("{0:F FF FFF FFFF}", dt); // "1 12 123 123" without zeroes
String.Format("{0:t tt}", dt); // "P PM" A.M. or P.M.
String.Format("{0:z zz zzz}", dt); // "-6 -06 -06:00" time zone
String.Format("{0:M/d/yyyy}", dt); // "3/9/2008"
String.Format("{0:MM/dd/yyyy}", dt); // "03/09/2008"
String.Format("{0:ddd, MMM d, yyyy}", dt); // "Sun, Mar 9, 2008"
String.Format("{0:dddd, MMMM d, yyyy}", dt); // "Sunday, March 9, 2008"
String.Format("{0:MM/dd/yy}", dt); // "03/09/08"
String.Format("{0:MM/dd/yyyy}", dt); // "03/09/2008"
DateTime dt = DateTime.ParseExact(your string date, "d/M/yyyy",
CultureInfo.InvariantCulture);
// for both "1/1/2000" or "25/1/2000" formats
string ndt = dt.ToString("MM/dd/yyyy");
use this namespace also: using System.Globalization;
Try this:
string d ="25/02/2012";
DateTime dtReturn = DateTime.MinValue;
DateTime.TryParseExact(d , "dd/MM/yyyy", dateFormat,
DateTimeStyles.AllowWhiteSpaces, out dtReturn)
will Return "2/25/2012 12:00:00 AM"

Working out start of financial year

Our financial year starts at 1st of April of every year. Therefore the current financial year is 1st of April 2011.
How do I get this date no matter what the current date is?
For example, today is 2011-06-24, how do I return 2011-04-30.
If today is 2012-02-05, I still need it to return 2011-04-30. However, if today is 2012-07-06, it should return 2012-04-30.
So basically, the year of the financial date should not change to the current year until the first of May is reached.
i.e. some examples
2011-03-05 = 2010-04-30
2011-04-06 = 2011-04-30
2010-01-15 = 2009-04-30
2015-09-01 = 2015-04-30
2020-12-25 = 2020-04-30
2021-02-26 = 2020-04-30
There is no built-in function, however it's easy to build yourself:
Get the current date
If it's april or later, use the year and set month and day to april 1st
If it's before april, use the year before, again april 1st.
You can simple use a static method:
public static GetStartOfFinancialYear() {
DateTime startOfYear = new DateTime( Datetime.UtcNow.Year, 4, 30 );
return
DateTime.UtcNow < startOfYear ?
startOfYear.AddYears(-1) : startOfYear;
}
in VB
public shared Function GetStartOfFinancialYear() As DateTime
Dim startOfYear As New DateTime(DateTime.Now.Year, 4, 30)
If DateTime.UtcNow < startOfYear Then
return startOfYear.AddYears(-1)
Else
return startOfYear
End If
End Function
Use the:
Dim dateTime__1 As New DateTime(DateTime.Now.AddMonths(-4).Year, 4, 30)
The Time Period Library for .NET includes the class Year with support of fiscal time periods.
You can define a fiscal time calendar with a custom year base month. The following sample uses October as the start of the fiscal year:
// ------------------------------------------------------------------------
public class FiscalTimeCalendar : TimeCalendar
{
// ----------------------------------------------------------------------
public FiscalTimeCalendar() :
base( new TimeCalendarConfig
{
YearBaseMonth = YearMonth.October, // October year base month
YearWeekType = YearWeekType.Iso8601, // ISO 8601 week numbering
YearType = YearType.FiscalYear// treat years as fiscal years
} )
{
} // FiscalTimeCalendar
} // class FiscalTimeCalendar
And that's the usage:
Collapse
// ----------------------------------------------------------------------
public void FiscalYearSample()
{
FiscalTimeCalendar calendar = new FiscalTimeCalendar(); // use fiscal periods
DateTime moment1 = new DateTime( 2006, 9, 30 );
Console.WriteLine( "Fiscal Year of {0}: {1}", moment1.ToShortDateString(),
new Year( moment1, calendar ).YearName );
// > Fiscal Year of 30.09.2006: FY2005
Console.WriteLine( "Fiscal Quarter of {0}: {1}", moment1.ToShortDateString(),
new Quarter( moment1, calendar ).QuarterOfYearName );
// > Fiscal Quarter of 30.09.2006: FQ4 2005
DateTime moment2 = new DateTime( 2006, 10, 1 );
Console.WriteLine( "Fiscal Year of {0}: {1}", moment2.ToShortDateString(),
new Year( moment2, calendar ).YearName );
// > Fiscal Year of 01.10.2006: FY2006
Console.WriteLine( "Fiscal Quarter of {0}: {1}", moment1.ToShortDateString(),
new Quarter( moment2, calendar ).QuarterOfYearName );
// > Fiscal Quarter of 30.09.2006: FQ1 2006
} // FiscalYearSample
The library includes also the classes Halfyear and Quarter with support of fiscal time periods.

Resources