i have a calendar in my site which take a start date and end date and pass them into a function who calculates the dates between .
lets sat we have the start date Mon Mar 29 2021 03:00:00 GMT+0300 (Eastern European Summer Time) and the end date is Mon Apr 05 2021 03:00:00 GMT+0300 (Eastern European Summer Time) ; this function should return ["30/3/2021","31/3/2021","1/4/2020","2/4/2020","3/4/2020","4/4/2020"]
let getDatesInRange = (start, end) => {
let dates = ref([])
let current = ref(start)
while current.contents <= end {
dates := dates.contents->Js.Array2.concat([current.contents->toUTCDateString])
current := {
let date = current.contents->toUTCDateString->Js.Date.fromString
date->Js.Date.setDate(date->Js.Date.getDate +. 1.0)->ignore
date
}
}
dates.contents
}
and this is toUTCDateString function which take a date and give the string version of it
let toUTCDateString = date => {
let date = date->External.unSafeCastToJsObject
date["toISOString"]()["split"]("T")[0]
}
These functions where working fine until The time has changed for Daylight Saving Time; we gain an hour so the day stuck there in for some reason
Any body face this issue before and who to deal with such time issues ?
Related
I am making a Alarm System for Mon, Tue, Wed, Thu, Fri Sat and Sun separate. Let's say i am setting an alarm for Tuesday than i need all Tuesdays of current Month and atleast for current year till the alarm is Off. I want to achieve it using moment.
I am using Package - 'moment-weekdaysin', this is not giving me proper result. It is giving me incorrect date.
code - moment().weekdaysInMonth('Monday')
I've not used moment-weedaysin before but it should share most of the same API as core moment. The code below will capture all Tuesday dates in the format of Month-Day-Year (MM-DD-YYY) from the current date til the end of the current year. You can change the parameter for any other days 1-7 (Monday-Sunday).
import moment from 'moment';
const getOccurrencesOfDayThisYear = (day = 1) => {
let startDate = moment();
const endOfYear = moment().endOf('year');
const extractedDates = [];
while (startDate.isBefore(endOfYear)) {
if (moment(startDate).day() == day) {
extractedDates.push(moment(startDate).format('MM-DD-YYYY'));
}
startDate = moment(startDate).add(1, 'days');
}
return extractedDates;
};
// 2 represents Tuesday; 1 being Monday and 7 being Sunday
getOccurrencesOfDayThisYear(2)
There might be a more sophisticated way of performing this through various moment methods.
it's very simple example:env.keyBy(value -> (...)) .window(TumblingProcessingTimeWindows.of(Time.hours(24))).addSink();
................
public Collection<TimeWindow> assignWindows(){
final long now = context.getCurrentProcessingTime();
long start = TimeWindow.getWindowStartWithOffset(now, offset, size);
// the value "now" is correct = 1603379120043 (Date in your timezone*: 10/22/2020, 12:05:20 PM GMT-0300 (-03))
// the value "start" is 1603324800000 (Date in your timezone*: 10/21/2020, 9:00:00 PM GMT-0300 (-03) : ???!!!!!
// I should started yesterday ???
// As the result:
public TimeWindow(long start, long end) {
this.start = start; //1603324800000 - Date in your timezone*: 10/21/2020, 9:00:00 PM GMT-0300 (-03)
this.end = end; //1603411200000 - Date in your timezone*: 10/22/2020, 9:00:00 PM GMT-0300 (-03)}
So, my job with 24h TumblingProcessingTimeWindows starting now at 10/22/2020, 12:05:20 PM will be finished today at 9:00:00 PM === 9 hours instead of 24 hours
Some solutions, please ?
By default, Flink's windows are aligned to the epoch, not to the time when they are created. So a 24 hour window will end at midnight UTC.
You can use the optional offset parameter to shift the window boundaries.
I have a format which returns me some dates and I need to parse it into something else which I find a little bit complicated.
The data format is Mon Dec 24 2018 9:00:00 as a startDate for example and Friday Dec 28 2018 17:00:00 as an endTime for example. What happens here is that I select I want someone to start on Monday at 9 until 17 everyday, but what my data does is makes it look like he's working non-stop.
I have tried mapping over it and putting it onto objects with days of the week and start and end times, but I ran into a problem because I create the object like
dates : {
monday: {
start: 9:00,
end: 18:00
},
tuesday: {
start:9:00,
end:18:00
}
// etc for everyday of the week
}
But, what if I only need Monday through Thursday, for example, that would be a problem. Anyone has any idea, how could I do that, in any other way? I was thinking about using moment.js.
I'm not sure if I understand your question, but I think you want to list all days between two different dates, here is an example of function that iterates over days by using moment.isSameOrBefore and moment.add functions, hope this help:
function toDays(startDateString, endDateString) {
const startDate = moment(startDateString, 'dddd MMM DD YYYY');
const endDate = moment(endDateString, 'dddd MMM DD YYYY');
const dates = {};
while(startDate.isSameOrBefore(endDate, 'day')) {
const currentDay = startDate.format('dddd');
dates[currentDay] = {start:'9:00', end:'18:00'};
startDate.add(1, 'days');
}
return dates;
}
const result = toDays('Monday Dec 24 2018', 'Friday Dec 28 2018');
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js"></script>
My code
for(n in data.values){
data.values[n].snapshot = new Date(data.values[n].snapshot);
data.values[n].value = parseInt(data.values[n].value);
console.log(data.values[n].snapshot);
}
here console.log shows perfect date in Chrome as 'Thu Aug 07 2014 14:29:00 GMT+0530 (India Standard Time)', but in Firefox it is showing as 'Invalid Date'.
If I console.log(data.values[n].snapshot) before the new Date line, it is showing date as
2014-08-07 14:29
How can I convert the date format to Firefox understandable way.
The Date object only officially accepts two formats:
Mon, 25 Dec 1995 13:30:00 GMT
2011-10-10T14:48:00
This means that your date 2014-08-07 14:29 is invalid.
Your date can be easily made compatible with the second date format though (assuming that date is yyyy-mm-dd hh:mm):
for(n in data.values){
n = n.replace(/\s/g, "T");
data.values[n].snapshot = new Date(data.values[n].snapshot);
data.values[n].value = parseInt(data.values[n].value);
console.log(data.values[n].snapshot);
}
my case is I have a Date obj the date inside is UTC time. However I want it to be changed to Japan time.
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Japan"));
calendar.setTime(someExistingDateObj);
System.out.println(String.valueOf(calendar.get(Calendar.HOUR_OF_DAY)) + ":" + calendar.get(Calendar.MINUTE));
the existingDateObj is mapped from db and db value is 2013-02-14 03:37:00.733
04:37
it seems the timezone is not working?
thanks for your time....
Your problem may be that you're looking at things wrong. A Date doesn't have a time zone. It represents a discrete moment in time and is "intended to reflect coordinated universal time". Calendars and date formatters are what get time zone information. Your second example with the Calendar and TimeZone instances appears to work fine. Right now, this code:
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Japan"));
System.out.println(String.valueOf(calendar.get(Calendar.HOUR)) + ":" + calendar.get(Calendar.MINUTE));
}
Reports:
0:32
That appears correct to me. What do you find wrong with it?
Update: Oh, perhaps you're expecting 12:32 from the above code? You'd want to use Calendar.HOUR_OF_DAY instead of Calendar.HOUR for that, or else do some hour math. Calendar.HOUR uses 0 to represent both noon and midnight.
Update 2: Here's my final attempt to try to get this across. Try this code:
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("H:mm a Z");
List<TimeZone> zones = Arrays.asList(
TimeZone.getTimeZone("CST"),
TimeZone.getTimeZone("UTC"),
TimeZone.getTimeZone("Asia/Shanghai"),
TimeZone.getTimeZone("Japan"));
for (TimeZone zone : zones) {
calendar.setTimeZone(zone);
format.setTimeZone(zone);
System.out.println(
calendar.get(Calendar.HOUR_OF_DAY) + ":"
+ calendar.get(Calendar.MINUTE) + " "
+ (calendar.get(Calendar.AM_PM) == 0 ? "AM " : "PM ")
+ (calendar.get(Calendar.ZONE_OFFSET) / 1000 / 60 / 60));
System.out.println(format.format(calendar.getTime()));
}
}
Note that it creates a single Calendar object, representing "right now". Then it prints out the time represented by that calendar in four different time zones, using both the Calendar.get() method and a SimpleDateFormat to show that you get the same result both ways. The output of that right now is:
22:59 PM -6
22:59 PM -0600
4:59 AM 0
4:59 AM +0000
12:59 PM 8
12:59 PM +0800
13:59 PM 9
13:59 PM +0900
If you used Calendar.HOUR instead of Calendar.HOUR_OF_DAY, then you'd see this instead:
10:59 PM -6
22:59 PM -0600
4:59 AM 0
4:59 AM +0000
0:59 PM 8
12:59 PM +0800
1:59 PM 9
13:59 PM +0900
It correctly shows the current times in Central Standard Time (my time zone), UTC, Shanghai time, and Japan time, respectively, along with their time zone offsets. You can see that they all line up and have the correct offsets.
sdf2 and sdf3 are equaly initialized, so there is no need for two of them.