Skip to content
View in the app

A better way to browse. Learn more.

FMForums.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Get UTC Time from Local Timestamp

Featured Replies

... feel free to report bugs & errors


// USAGE : LocalTimeStampToUTC ( fm_TimeStamp ; fm_TimeStampDateFormat ; fm_UtcOutputPattern )

// 20111115 clem

// version 1.2

//

// fm_TimeStampDateFormat => 1 = dd/MM/yyyy; 2 = MM/dd/yyyy; 3 = yyyy/MM/dd

// fm_UtcOutputPattern => see http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

//

// EXAMPLE = LocalTimeStampToUTC ( 01/01/2011 00:07:20 ; 1 ; "dd/MM/yyyy HH:mm:ss" ) [ my machine is located in Time Zone GMT + 1 ]

// RESULT => 31/12/2010 23:07:20



import java.text.SimpleDateFormat

import static java.util.Calendar.*



if (fm_TimeStamp && fm_UtcOutputPattern ){



    arrTxt = fm_TimeStamp.split(/[^0-9]/)

    def arrInt = []

    arrTxt.each{ arrInt.add(it as Integer)}



    def dateMap = [:]

    tsdf = ! fm_TimeStampDateFormat ? 1 : fm_TimeStampDateFormat as Integer

    switch ( tsdf ) {

	    case 2: dateMap = [y:arrInt[2], m:arrInt[0], d:arrInt[1]]; break;

	    case 3: dateMap = [y:arrInt[0], m:arrInt[1], d:arrInt[2]]; break;

	    default: dateMap = [y:arrInt[2], m:arrInt[1], d:arrInt[0]]

    }

    def timeMap = [h: arrInt[3], mn: arrInt[4], s: arrInt[5]]

    def tsMap = dateMap += timeMap



    // Month value is 0-based. e.g., 0 for January.

    def localCal = new GregorianCalendar()

    localCal.with{

	    set YEAR, tsMap["y"]

	    set MONTH, tsMap["m"] - 1

	    set DAY_OF_MONTH, tsMap["d"]

	    set HOUR_OF_DAY, tsMap["h"]

	    set MINUTE, tsMap["mn"]

	    set SECOND, tsMap["s"]

    }



    def utcCal = new GregorianCalendar(TimeZone.getTimeZone("GMT"))

    utcCal.timeInMillis = localCal.timeInMillis



    year = utcCal.get(YEAR)

    month = utcCal.get(MONTH)

    day = utcCal.get(DAY_OF_MONTH)



    h = utcCal.get(HOUR)

    h = ! utcCal.get(AM_PM) && ! fm_TimeStamp.toUpperCase().contains('PM') ? h : h + 12

    m = utcCal.get(MINUTE)

    s = utcCal.get(SECOND)



    def sdf = new SimpleDateFormat( fm_UtcOutputPattern )

    def cal = new GregorianCalendar(year, month, day, h, m, s)



    return sdf.format(cal.time)

}

see "Date & Time patterns" from the SimpleDateFormat class

Edited by clemhoff

Clem

If I feed it this 11/17/2011 3:10:42 PM which is Get(CurrentTimeStamp) I get an answer of 11/05/2012 02:10:42 Friday AD

I am sat in GMT btw.

Also 21/06/2011 00:07:20 gives me 20/06/2011 23:07:20 Monday AD British Summer Time, which is correct

  • Author

Hi John,

If I feed it this 11/17/2011 3:10:42 PM which is Get(CurrentTimeStamp) I get an answer of 11/05/2012 02:10:42 Friday AD

Because you certainly didn't enter the value "2" for parameter "fm_LocalDateFormat".

fm_LocalDateFormat => 1 = dd/MM/yyyy; 2 = MM/dd/yyyy; 3 = yyyy/MM/dd

Also 21/06/2011 00:07:20 gives me 20/06/2011 23:07:20 Monday AD British Summer Time, which is correct

... because 21/06/2011 is fm_LocalDateFormat = 1 and the string TimeStamp too.

That is indeed my mistake, but the issue is actually with not recognising the PM part correctly. 03:00:00 PM should be 15:00:00

Needs a test if the timestamp string contains PM

  • Author

John; thank you for reporting !

I added the test and updated the function.

Create an account or sign in to comment

Important Information

By using this site, you agree to our Terms of Use.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.