I Can Select Time at Date Picker When I Get Value from That in Utc So How to Convert Utc Time to Gmt+05:30 to Date

i can select time at date picker when i get value from that in UTC so how to convert UTC time to GMT+05:30 to date?

You need to do like this

UIDatePicker display Local DateTime but it returns UTC DateTime. So you need to convert it to Local for getting actual Local Date and time.

func GetLocalStringDateFromUTC(date:Date)-> String{

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss" //Your date format
dateFormatter.timeZone = TimeZone(abbreviation: "GMT+5:30") //Current time zone different
let myString = dateFormatter.string(from: date) //according to date format your date string
return myString //Convert String to Date
}

// For Get date from string
func GetDateFromString(date_string:String,dateFormat:String)-> Date{

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = dateFormat //Your date format
dateFormatter.timeZone = TimeZone(abbreviation: "GMT+0:00") //Current time zone
let date = dateFormatter.date(from: date_string) //according to date format your date string
return date! //Convert String to Date
}

// For Get time from Date
func GetTimefromDate(date_string:String,dateFormat:String)-> String{

let dateFormatter_db = DateFormatter()
dateFormatter_db.dateFormat = dateFormat
let dateFormatter_eventDate = DateFormatter()
dateFormatter_eventDate.dateFormat = "hh:mm a" //Your time format
let date = dateFormatter_db.date(from: date_string)
return dateFormatter_eventDate.string(from: date!)
}

Use like this

let date = datePicker.date  // Pass your DatePicker Date Here

print(GetLocalStringDateFromUTC(date: date)) // Local String Date from UTC
print(GetDateFromString(date_string: GetLocalStringDateFromUTC(date: date), dateFormat: "yyyy-MM-dd HH:mm:ss")) // Local Date() from UTC
print(self.GetTimefromDate(date_string: self.GetLocalStringDateFromUTC(date: datePicker.date), dateFormat: "yyyy-MM-dd HH:mm:ss")) // Local Time from UTC

Output:

Sample Image

Convert Date/Time for given Timezone - java

For me, the simplest way to do that is:

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

//Here you say to java the initial timezone. This is the secret
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
//Will print in UTC
System.out.println(sdf.format(calendar.getTime()));

//Here you set to your timezone
sdf.setTimeZone(TimeZone.getDefault());
//Will print on your default Timezone
System.out.println(sdf.format(calendar.getTime()));

Change only the local TimeZone to Default UTC using Javascript

You can try the following piece of code

date.setMinutes((date.getTimezoneOffset() * -1));

No need of localization, use this code just before doing any service call. It will pass you the exact date what you selected in the datepicker.

It will work in all timezone (+) and (-),

Example: 1990-01-01 00:00:00 GMT+0530 (India Standard Time), the above said code covert it as 1990-01-01 05:30:00 GMT+0530 (India Standard Time). While on Service it converts it as 1990-01-01 00:00:00.

I think it will solve your problem.

JSON Stringify changes time of date because of UTC

Recently I have run into the same issue. And it was resolved using the following code:

x = new Date();
let hoursDiff = x.getHours() - x.getTimezoneOffset() / 60;
let minutesDiff = (x.getHours() - x.getTimezoneOffset()) % 60;
x.setHours(hoursDiff);
x.setMinutes(minutesDiff);

Convert date to another timezone in JavaScript

Here is the one-liner:

function convertTZ(date, tzString) {
return new Date((typeof date === "string" ? new Date(date) : date).toLocaleString("en-US", {timeZone: tzString}));
}

// usage: Asia/Jakarta is GMT+7
convertTZ("2012/04/20 10:10:30 +0000", "Asia/Jakarta") // Tue Apr 20 2012 17:10:30 GMT+0700 (Western Indonesia Time)

// Resulting value is regular Date() object
const convertedDate = convertTZ("2012/04/20 10:10:30 +0000", "Asia/Jakarta")
convertedDate.getHours(); // 17

// Bonus: You can also put Date object to first arg
const date = new Date()
convertTZ(date, "Asia/Jakarta") // current date-time in jakarta.

How to store date/time and timestamps in UTC time zone with JPA and Hibernate

Since Hibernate 5.2, you can now force the UTC time zone by adding the following configuration property into the properties.xml JPA configuration file:

<property name="hibernate.jdbc.time_zone" value="UTC"/>

If you're using Spring Boot, then add this property to your application.properties file:

spring.jpa.properties.hibernate.jdbc.time_zone=UTC


Related Topics



Leave a reply



Submit