Convert timezone specific date to locale using moment - momentjs

I have the following data for a timezone:
{
"Id": "Pacific Standard Time",
"DisplayName": "(UTC-08:00) Pacific Time (US & Canada)",
"StandardName": "Pacific Standard Time",
"DaylightName": "Pacific Daylight Time",
"BaseUtcOffset": "-08:00:00",
"AdjustmentRules": [{
"DateStart": "0001-01-01T00:00:00Z",
"DateEnd": "2006-12-31T00:00:00Z",
"DaylightDelta": "01:00:00",
"DaylightTransitionStart": {
"TimeOfDay": "0001-01-01T02:00:00Z",
"Month": 4,
"Week": 1,
"Day": 1,
"DayOfWeek": 0,
"IsFixedDateRule": false
},
"DaylightTransitionEnd": {
"TimeOfDay": "0001-01-01T02:00:00Z",
"Month": 10,
"Week": 5,
"Day": 1,
"DayOfWeek": 0,
"IsFixedDateRule": false
},
"BaseUtcOffsetDelta": "00:00:00"
}, {
"DateStart": "2007-01-01T00:00:00Z",
"DateEnd": "9999-12-31T00:00:00Z",
"DaylightDelta": "01:00:00",
"DaylightTransitionStart": {
"TimeOfDay": "0001-01-01T02:00:00Z",
"Month": 3,
"Week": 2,
"Day": 1,
"DayOfWeek": 0,
"IsFixedDateRule": false
},
"DaylightTransitionEnd": {
"TimeOfDay": "0001-01-01T02:00:00Z",
"Month": 11,
"Week": 1,
"Day": 1,
"DayOfWeek": 0,
"IsFixedDateRule": false
},
"BaseUtcOffsetDelta": "00:00:00"
}],
"SupportsDaylightSavingTime": true
}
and the following date:
2019-01-05T07:30:07Z
Using moment, i'd like to convert this to my current locale which is
en-GB
How do i go about doing this with momentjs?
the problem seems to be that the date format i have is not compatible with moment.
I've prepared a jsfiddle with variables that i have access to:
http://jsfiddle.net/jimmyt1988/xe7ha5ms/1/

Your input is in ISO 8601 format recognized by moment. You can parse it using moment.tz:
The moment.tz constructor takes all the same arguments as the moment constructor, but uses the last argument as a time zone identifier.
then you can convert it to desired timezone using tz():
The moment#tz mutator will change the time zone and update the offset.
You can set locale on a moment object using locale() and then use format() to display value.
Here a code sample:
console.log(
moment.tz('2019-01-05T07:30:07', 'America/Los_Angeles') // parse input in given tz
.tz('Europe/London') // Convert moment to desired zone
.locale('en-gb') // set en-gb locale
.format('LLLL') // display result
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment-with-locales.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.23/moment-timezone-with-data-2012-2022.min.js"></script>

Figured it out, by using the utcOffset method:
http://jsfiddle.net/jimmyt1988/xe7ha5ms/2/
moment("2019-01-05T07:30:07Z")
.utcOffset("-08:00:00")
.format("DD/MM/YYYY HH:mm:ss");

Related

How can I use datetime format without have interpolation escape to false

Doing this:
// JSON
{
"intlDateTime": "On the {{val, datetime}}",
}
i18next.t('intlDateTime', { val: new Date(Date.UTC(2012, 11, 20, 3, 0, 0)) });
// --> On the 12/20/2012
without interpolation escape to false
ends with a display of / instead of "/"
How can we use date format and avoid xss attacks at the same time ?
To solve the wrong display, I need to write:
{t('lastModified', {
val: new Date(Date.UTC(2012, 11, 20, 3, 0, 0)),
interpolation: { escapeValue: false },
})}
which I would like to avoid.

How can I change date formats "2022-06-04T00:00:00.000Z" to "1525844100000" for react highcharts

I've the following date: "2022-06-04T00:00:00.000Z" format, We've need of date calculation for highcharts in this numeric type format "1525844100000" so how can I calculate in react-highcharts
xAxis: {
type: 'datetime',
ordinal: false,
startOnTick: false,
endOnTick: false,
minPadding: 0,
maxPadding: 0,
Date: false,
tickInterval: 4 3600 1000,
minRange: 1 24 3600000,
dateTimeLabelFormats: {
day: '%l %P',
hour: '%l %P'
},
offset: 0,
},
series: [
{
"data": [[1.424304e+12, 0.25]],
color: '#FFA749',
},
],
"highcharts": "^6.1.1",
"react-highcharts": "^16.0.2",
I've given above my charts details
In order to convert ISO-8601 to timestamp format, use getTime() JS method.
let timestamp = new Date('2022-06-04T00:00:00.000Z').getTime()
// expected output: 1654300800000
Demo: https://jsfiddle.net/BlackLabel/vnLdum1k/

How to save flutter TimeOfDay to firebase?

I am working on a flutter application where I am showing a TimeRangepicker using this plugin: time_range_picker and I am getting TimeOfDay(09:00) + TimeOfDay(12:00) as a result by using this code:
onPressed: () async {
TimeRange result = await showTimeRangePicker(
use24HourFormat: false,
interval: Duration(minutes: 30),
context: context,
start: TimeOfDay(hour: 9, minute: 0),
end: TimeOfDay(hour: 12, minute: 0),
disabledTime: TimeRange(
startTime: TimeOfDay(hour: 23, minute: 0),
endTime: TimeOfDay(hour: 5, minute: 0)),
disabledColor: Colors.red.withOpacity(0.5),
strokeWidth: 4,
ticks: 24,
ticksOffset: -7,
ticksLength: 15,
ticksColor: Colors.grey,
labels: [
"12 pm",
"3 am",
"6 am",
"9 am",
"12 am",
"3 pm",
"6 pm",
"9 pm"
].asMap().entries.map((e) {
return ClockLabel.fromIndex(
idx: e.key, length: 8, text: e.value);
}).toList(),
labelOffset: 35,
rotateLabels: false,
padding: 60);
print("${result.startTime} + ${result.endTime}");
},
But the only problem is I can find an appropriate way to save this to firebase, I don't need date all I need is TimeOfDay.
There is no specific data type in Firestore for storing a time of day, so you'll have to map it to one of the existing types.
Some common mapping are:
Store the time of day as a string value, such as "07:55:00.000" (if you care up to millisecond precision)
Store the time of day as an offset from midnight, such as 28500 (as the number of seconds from midnight until 7:55 AM)
Store the time of day as a the time part of a fixed day in a Timestamp field. This is essentially a variant of the previous approach, with the offset being from the start of the epoch. Say that you pick the start of the epoch (January 1, 1970) as the fixed day, then the Timestamp's value would be 28500000 (in milliseconds).
All of these have valid use-case, so pick whichever one works best for the use-cases of your app.
In the common use case whereby only the hour and minute are required a simple map of these values will suffice.
Map timeOfDayToFirebase(TimeOfDay timeOfDay){
return {
'hour':timeOfDay.hour,
'minute':timeOfDay.minute
}
}
TimeOfDay firebaseToTimeOfDay(Map data){
return TimeOfDay(
hour: data['hour'],
minute: data['minute']);
}
var myTimeOfDayObject=TimeOfDay.now();
firebase.update({'time': timeOfDayToFirebase(myTimeOfDayObject)});
This way you don't have to worry about managing offsets, unintended locale changes and so forth.
You can use something like this
class ClassModel {
TimeOfDay time;
ClassModel({
required this.time,
});
Map<String, dynamic> toMap() {
return <String, dynamic>{
'time': (time.hour.toString() + "*" + time.minute.toString()),
};
}
factory ClassModel.fromMap(Map<String, dynamic> map) {
return ClassModel(
time: TimeOfDay(
hour: int.parse(map['time'].toString().split("*").first),
minute: int.parse(map['time'].toString().split("*").last)),
);
}
}

Compare Date in Condition [Vega-Lite]

I'm super new to this and trying out Observable and Vega-Lite for the first time. The following code works when the datum is a quantitative string but not when it's a date.
vl.color({"condition":{"test":"datum['dateAnnounced']<'2020-03-15'","value":"black"}, "value":"red"})
Can someone show me the correct way to compare Date fields, please? Thanks in advance!
condition.test accepts Predictate. It has string format which you're using, but also it could be an object like this:
condition: {
test: {
field: 'dateAnnounced', // your date field
lt: { year: 2020, month: 3, date: 15 } // 'less than' a date in Vega DateTime format
}
}
They call this object "field predicate". It has field prop and a comparison operator lt or gt or equal, etc.
There are some examples from Field Predicate section of the doc:
{"field": "car_color", "equal": "red"} // car_color === red
{"field": "height", "lt": 180} // height < 180
{"field": "date", // 2006-01-01 < date && date < 2008-02-20
"range": [
{"year": 2006, "month": "jan", "date": 1},
{"year": 2008, "month": "feb", "date": 20}
]
}

Format dateTime from json string

I have some JSON being returned... how can I format these date time strings using c# and JS?
{
"time": 1469257200,
"summary": "Partly cloudy in the morning.",
"icon": "partly-cloudy-day",
"sunriseTime": 1469279237,
"sunsetTime": 1469330841,
"moonPhase": 0.63,
...... ..... ...
},
and help is appreciated!!!
was easy once I realized that is a unix timestamp:
public static DateTime UnixTimeStampToDateTime(double unixTimeStamp)
{
// Unix timestamp is seconds past epoch
System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
dtDateTime = dtDateTime.AddSeconds(unixTimeStamp).ToLocalTime();
return dtDateTime;
}

Resources