How to convert date to different timezone - timezone

I have a date in string format as:
var x = '2017-07-11 12:00';
var timeZone = 'America/New_York'
Now how can I format date according to timeZone? What I am expecting is:
var y = someMagicalFunction(x,timeZone)
// y = '2017-07-10 14:30'
Also reverse of it:
var x = reverseOfMagicalFunction(y, 'Asia/Kolkata');
// x = '2017-07-11 12:00';
I have used moment.tz to get results when x was of type Date.
var x1 = x.toISOString();
var y = moment.utc(x).tz(timezone).format('YYYY/MM/DD hh:mm:ss');
but was not able to get ReverseMagicalFucntion in that case. Any help?

When you are using momentjs, you can try something like (Check Here):
moment(date).tz('Europe/Berlin').format(format);

Related

Assign values of an Int to two separate variables [duplicate]

This question already has answers here:
How to split an Int to its individual digits?
(11 answers)
Closed 3 years ago.
say for example i have an Int var firstInt = 23 what i need is i want to assign the value of firstInt to two separate variables so the output would be var x = 2 and var y = 3. i tried converting the firstInt to a string like so var strFirstInt = String(firstInt) and wanted to assign the first index of the string to a different variable and the second index to another variable and convert them to Int but i couldn't pick the string by index. so any ideas how to do this?
You can use .compactMap from String like this :
let numberInt = 23
let digits = String(numberInt).compactMap{ $0.wholeNumberValue}
Response :
[2, 3]
And with this array, you put the first member to the first var and seconds to another:
var x = digits[0]
var y = digits[1]
print("The decade is \(x) and units is \(y)")
Response:
The decade is 2 and units is 3
Convert the firstInt to String and then to Array,
var firstInt = 23
let arr = Array(String(firstInt)).map({ String($0 )})
Next, get the elements as per the index from array, i.e.
var x = Int(arr[0])
var y = Int(arr[1])

Can we support number localization using moment.js

var x = 3500;
var y = x.momentJSApi("hindi");
console.log(y);//y = ३,५००
Expected :- ३,५००(based on locale the y value should change)
Want to support number localization.

How do you use parse string in swift?

An issue here to me that if i use parse string for the result of calculator program for instance,
4.5 * 5.0 = 22.5
how can I use splitting here to depart decimal part from result?
Assuming you're working with strings only :
var str = "4.5 * 5.0 = 22.5 "
// Trim your string in order to remove whitespaces at start and end if there is any.
var trimmedStr = str.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
// Split the string by " " (whitespace)
var splitStr = trimmedStr.componentsSeparatedByString(" ")
// If the split was successful, retrieve the last past (your number result)
var lastPart = ""
if let result = splitStr.last {
lastPart = result
}
// Since it's a XX.X number, split it again by "." (point)
var splitLastPart = lastPart.componentsSeparatedByString(".")
// If the split was successful, retrieve the last past (your number decimal part)
var decimal = ""
if let result = splitLastPart.last {
decimal = result
}
Use modf to extract decimal part from result.
Objective-C :
double integral = 22.5;
double fractional = modf(integral, &integral);
NSLog(#"%f",fractional);
Swift :
var integral:Double = 22.5;
let fractional:Double = modf(integral,&integral);
println(fractional);
Want only interger part from double of float
Want only integer value from double then
let integerValue:Int = Int(integral)
println(integerValue)
Want only integer value from float then
let integerValue:Float = Float(integral)
println(integerValue)

How can I find min and max values in a (multidimensional?) array

I have an array that is created like this in Swift
class TableViewCell: UITableViewCell, TKChartDelegate {
var xAxisDates = [] --> filled with fetch from Coredata
var yAxisValues = [] --> filled with fetch from Coredata
var maximumValue = maxElement(yAxisValues) -> error: TableViewCell.Type' does not have a member named 'yAxisValues'
func chart (){
var dataPointSeries = [TKChartDataPoint]()
for var i = 0; i < xAxisDates.count; ++i {
dataPointSeries.append(TKChartDataPoint(x: xAxisDates[i], y: yAxisValues[i]))
println(dataPointSeries)
}
<--more code setting up the chart-->
}
The console output is this:
[x = '2014-11-25 02:00:00 +0000', y = '61', name = '(null)', x = '2014-11-25 02:00:00 +0000', y = '57', name = '(null)']
I would like to find the max and min value of the y's.
I have tried maxElement(dataPointSeries(x)), but that does not work. Any idea how I could do this? I can't seem to find any solution.
All you want is the minimum and maximum values of the y elements, there is no need to consider this in the context of the multi-dimensional array. All you need do is:
let minimumYValue = minElement(yAxisValues)
let maximumYValue = maxElement(yAxisValues)

DateTime conversion to Excel Date in f#

I read on stackoverflow that easiest way to convert a DateTime variable back to Excel date was simply to do:
let exceldate = int(DateTime)
Admittedly this was in c# and not f#. This is supposed to work as the decimals represent time and int part represents date. I tried this and f# comes back with the error:
The type 'DateTime' does not support a conversion to the type 'int'
So how do I convert back to excel date?
More specificly, I m trying to create a vector of month 1st for a period between start date and end date. Both vector output and start date and end date are floats, i.e. excel dates. Here my clumsy first attempt:
let monthlies (std:float) (edd:float) =
let stddt = System.DateTime.FromOADate std
let edddt = System.DateTime.FromOADate edd
let vecstart = new DateTime(stddt.Year, stddt.Month, 1)
let vecend = new DateTime(edddt.Year, edddt.Month, 1)
let nrmonths = 12 * (edddt.Year-stddt.Year) + edddt.Month - stddt.Month + 1
let scaler = 1.0 - (float(stddt.Day) - 1.0) / float(DateTime.DaysInMonth(stddt.Year , stddt.Month))
let dtsvec:float[] = Array.zeroCreate nrmonths
dtsvec.[0] <- float(vecstart)
for i=1 to (nrmonths-1) do
let temp = System.DateTime.FromOADate dtsvec.[i-1]
let temp2 = temp.AddMonths 1
dtsvec.[i] = float temp2
dtsvec
This doesnt work because of the conversion issue and is rather complicated and imperative.
How do I do the conversion? How can I do this more functionally? Thanks
Once you have the DateTime object, just call ToOADate, like so:
let today = System.DateTime.Now
let excelDate = today.ToOADate()
So your example would end up like so:
let monthlies (std:float) (edd:float) =
let stddt = System.DateTime.FromOADate std
let edddt = System.DateTime.FromOADate edd
let vecstart = new System.DateTime(stddt.Year, stddt.Month, 1)
let vecend = new System.DateTime(edddt.Year, edddt.Month, 1)
let nrmonths = 12 * (edddt.Year-stddt.Year) + edddt.Month - stddt.Month + 1
let scaler = 1.0 - (float(stddt.Day) - 1.0) / float(System.DateTime.DaysInMonth(stddt.Year , stddt.Month))
let dtsvec:float[] = Array.zeroCreate nrmonths
dtsvec.[0] <- vecstart.ToOADate()
for i=1 to (nrmonths-1) do
let temp = System.DateTime.FromOADate dtsvec.[i-1]
let temp2 = temp.AddMonths 1
dtsvec.[i] = temp2.ToOADate()
dtsvec
In regards to getting rid of the loop, maybe something like this?
type Vector(x: float, y : float) =
member this.x = x
member this.y = y
member this.xDate = System.DateTime.FromOADate(this.x)
member this.yDate = System.DateTime.FromOADate(this.y)
member this.differenceDuration = this.yDate - this.xDate
member this.difference = System.DateTime.Parse(this.differenceDuration.ToString()).ToOADate
type Program() =
let vector = new Vector(34.0,23.0)
let difference = vector.difference

Resources